View Single Post
 
Old 06-21-2023, 12:34 PM
ksor ksor is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 78
ksor is on a distinguished road
Default Inserting a Footer if it doesn't exist ???

I'm running MSOffice 365 Home edition.

I have 1000's of Word document where I want to insert a Footer showing FILENAME and PAGE of NUMPAGES.

I plan to do this

0) Add the footer with the needed FIELDS to the template
1) Run ONCE a treverse the hieraki of folders with the files
2) check if Footer exists already in each file

If NOT then
insert the FIELDS,
UPDATE the fields
SAVE the document again
else leave the file be and treverse to next

I have the new Footer inserted in the template and the treversing to function !

But the ONE-time-use subrutine to open a document and check for existing Footer is BAD !

I use this code for handlin a file from the hieraki:

Code:
Sub insertFooterIfMissing(file As String)
    Dim appWrd As New Word.Application
    Dim doc As Word.Document
    Dim footer As HeaderFooter
'    Set appWrd = CreateObject(Word.Application)
    ' Open the Word document
    Set doc = appWrd.Documents.Open(file)     <<<<<<<< (A) 
    ' Check if theapppWrd footer exists
    If doc.Sections(1).Footers(wdHeaderFooterPrimary).Exists Then  <<<<<< (B)
        ' just leave it be
    Else
        ' insert the Footer here AND update the Footer
        Debug.Print "Nu indsættes Footer i : " & file
    End If
    ' Close the document
    doc.Close
    Set doc = Nothing
    Set appWrd = Nothing
 End Sub

(A) this line is failing showing "macros is not allowed" in a Msgbox
(B) is ALWAYS true eventhough the document has NO Footer ... not that I can see at least !

What do I do wrong ?
Reply With Quote