I've build some code for traversing a folder hieraki returning a file path for each Word file in the hieraki - it work fine.
For each returned file name I want to insert a new Footer with FILENAME, PAGE, NUMPAGE and some accompany text.
I use this code for processing each file:
Sub insertFooterIfMissing(file As String)
Dim appWrd As New Word.Application
Dim doc As Word.Document
Dim footer As HeaderFooter
' Open the Word document
Set doc = appWrd.Documents.Open(file) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (A)
' Check if the apppWrd footer exists
'If doc.Sections(1).Footers(wdHeaderFooterPrimary).Exists Then
If Len(doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text) = 1 Then
' insert the Footer here AND update the Footer
'Debug.Print "Nu indsættes Footer i : " & file
With Selection
.WholeStory
' Delete existing Footer - if any
.Delete Unit:=wdCharacter, Count:=1
' Insert new fields in Footer
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
' move to the right margin and write "Side "
.TypeText Text:=vbTab & vbTab & "Side "
' and insert the Page field
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
' write " af "
.TypeText Text:=" af "
' and insert NumPages field
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
.Select
.Fields.Update
End With
Else
' Just leave it be
End If
UpdateFieldsInFooter doc
' Save the documente with the new updated Footer-fields
doc.Save
' Close the document
doc.Close
Set doc = Nothing
Set appWrd = Nothing
End Sub
Sub UpdateFieldsInFooter(doc As Word.Document)
Dim Scn As Section, HdFt As HeaderFooter
'Update fields in footers
With doc
'Loop thru all Sections
For Each Scn In .Sections
'Loop thru all Footers in Section
For Each HdFt In Scn.Footers
With HdFt
.Range.Fields.Update
End With
Next
Next
End With
End Sub
The line marked (A) gives a msgbox telling me the document is open with macros DISABLED and I have to click on OK for the code to run further !!!!!
I just want the doc to open, let the Footer be inserted, updating the fields in the footer and save the doc again - to process the next file from the folder hieraki.
YES, all the 1000's of Word documents in the folder hieraki have macros in the referenced Normal.dotm !
But how can I secure opening each document with macros ENABLED or at least get rid of that Msgbox telling me macros are disabled so I can do the changing of the footer and the document AFTER saving can use its macros.
I think it has to do with checkmarks in the "Center for security ..." ... but I can't figure out how to set them to get it right - TEACH ME !!!!