Share via

Word Documents are disabled macros when I try to open them via VBA

KeldSor 396 Reputation points
2023-06-24T06:36:12.76+00:00

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 !!!!

Microsoft 365 and Office | Development | Other
0 comments No comments

3 answers

Sort by: Most helpful
  1. KeldSor 396 Reputation points
    2023-07-08T07:48:37.99+00:00

    SOLVED - the code CAN NOT be run from the Normal.dotm without these problems !

    I moved the exact same code to a module in Access - and it run without any problems at all !

    I see NO reason why !

    Was this answer helpful?

    0 comments No comments

  2. 2023-06-24T15:46:55.3833333+00:00

    نعم اجابة مقبولة

    Was this answer helpful?

    0 comments No comments

  3. John Korchok 232.1K Reputation points Volunteer Moderator
    2023-06-24T15:42:48.9+00:00

    If you're confident that you have no malicious code on your system, temporarily set File>Options>Trust Center>Trust Center Settings>Macro Settings to Enable all macros. Run your process, then reset Macro Settings back to a Disable parameter.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.