Protected word template - vb/macro to update footer

tmc

Joined
Jan 9, 2017
Messages
4
Reaction score
0
I have a word template that is protected, trying to set the footer so that it updates to the file name when opening and closing the document. I can't seem to figure out the correct macro or vb for it.
The closest I've gotten is this:

' If document is protected, unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = ActiveDocument.FullName
End If

However, it does not update the footer with the file name at opening or closing the docx.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
Why not use a FILENAME field in the footer? No macros required.
 

tmc

Joined
Jan 9, 2017
Messages
4
Reaction score
0
Tried the filename in the footer. Doesn't update when the filename is changed due to the required protection on the doc.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
With the FILENAME field in place, you could use a macro coded as:
Code:
Private Sub Document_Open()
Application.ScreenUpdating = False
With ActiveDocument
  .PrintPreview
  .ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub
 

tmc

Joined
Jan 9, 2017
Messages
4
Reaction score
0
macropod: that did not work.

I can't even figure out how to unprotect the footer field. Word is much more convulted than excel.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
There is nothing that needs unprotecting via the macro. Once the field is in the footer (which you have to unprotect to do - meaning it should be done to the form's template, not to each form individually), the macro I posted is all that's needed to update it. Simply add it to the template's 'ThisDocument' code module. Added there, you can even save the documents in the docx format, since they don't need the macro. Furthermore, by using the field, the document name in the footer will update if ever the filename/path is changed - which code along the lines of what you were trying to do would be incapable of.
 

tmc

Joined
Jan 9, 2017
Messages
4
Reaction score
0
Nothing suggested has worked. :(
The template is set to restricted formatting and editing this is to prevent the template from being changed except in the fields indicated. Doing a file save as then saving the dotx with a new name as a docx does not update the footer. Closing the document and reopening the document does not change the footer. However, going into print preview then closing print preview does update the footer correctly.
I'm out of ideas...
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
Did you add the macro to the template's 'ThisDocument' code module?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top