![]() |
#1
|
|||
|
|||
![]()
Hi,
I know that if Word form is protected, the header/footer cannot be altered, is there any other ways to accomplish it or using VBA, etc. THanks |
#2
|
||||
|
||||
![]()
You could do that with code like:
Code:
Sub ModifyHeader() Application.ScreenUpdating = False Dim Pwd As String, pState As Variant, Rng As Range, StrTxt As String With ActiveDocument Set Rng = .Sections(1).Headers(wdHeaderFooterPrimary).Range Rng.End = Rng.End - 1 StrTxt = InputBox("Please input the modified header text.", "Header Update", Rng.Text) If Trim(StrTxt) = "" Then Exit Sub pState = False If .ProtectionType <> wdNoProtection Then Pwd = InputBox("Please enter the Password", "Password") pState = .ProtectionType .Unprotect Pwd End If Rng.Text = StrTxt If pState <> wdNoProtection Then .Protect Type:=pState, NoReset:=True, Password:=Pwd End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Many thanks macropod,
I have added your script on VBA in Word file, pw is 123 for testing, but it seems missing something, after finishing the word file, how can I call the password field to unprotect the header, I uploaded the test file for reference. |
#4
|
||||
|
||||
![]()
A couple of points:
1. Files in the docx format cannot contain macros, so there isn't one in your attachment. 2. Files using content controls don't need forms protection, so that's not necessary unless you have other reasons for using it. As for the macro, you could run it via Alt-F11, or you could rename it to: Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) insert as the first line: If ContentControl.Title <> "MyTitle" Then Exit Sub and place the code in your document's 'ThisDocument' code module. You would then apply the title 'MyTitle' to whatever content control you want to run the macro from. You can, of course, use something other than 'MyTitle' in both the code and the document. If you don't want the password prompt, you could replace: Pwd = InputBox("Please enter the Password", "Password") with: Pwd = "123" That said, if all you want to achieve is having the content of one of your content controls replicated in the header, you don't need a macro. All you need do is apply a unique character Style to the content control, then use a STYLEREF field in the header to reference that Style.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thanks Macropod,
Yes, docx cannot run macro.. Anyway, the original purpose is we want to set a form template and just let users to fill the Name, address, tel field only, we don't want users to modify the format in body.... and also, if we allow users to edit header under protection form condition, is your above VBA accomplish it. Many thanks. |
#6
|
||||
|
||||
![]()
No, that is not what I said. A docx file can run any macro that is in its template.
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
tanias | Word | 2 | 06-30-2014 11:55 AM |
![]() |
Snvlsfoal | Word | 1 | 07-12-2011 03:19 AM |
How to remove this paragraph (line) form the header? | Jamal NUMAN | Word | 1 | 04-14-2011 06:35 AM |
Document protection | Daved2424 | Word | 0 | 11-17-2010 01:22 AM |
Form field to automatically be added to header? | razberri | Word VBA | 3 | 02-22-2010 03:48 PM |