• WordBasic.DisableAutoMacros (VBA)

    Author
    Topic
    #426774

    In a number of recent threads, folkes have been wondering why AutoOpen is being invoked despite using WordBasic.DisableAutoMacros to disable AutoMacros.

    Under normal circumstances, DisableAutoMacros seems to do what is intended.
    Attempting to prove this, I constructed the crude example below.

    1. Create a NEW Word template and include the code below.
    2. Run the macros in the order indicated.

    I believe that you will see the expected behavior.

    Public Sub AutoOpen()
        MsgBox "AutoOpen in " & MacroContainer.FullName
    End Sub
    
    Option Explicit
    ' Run this first
    Public Sub RunMeFirst()
        Dim docWord As Word.Document
        Dim strFile As String
        
        Application.ScreenUpdating = False
        System.Cursor = wdCursorWait
        strFile = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator & CDbl(Now)
        
        ' Disable Auto macros
        WordBasic.DisableAutoMacros 1
        Set docWord = ActiveDocument
        With docWord
            .AttachedTemplate = MacroContainer.FullName
            .SaveAs FileName:=strFile, addtorecentfiles:=False
        End With
        Debug.Print "Saved: " & strFile
        Documents.Add Template:=MacroContainer.FullName
        docWord.Close
        Documents.Open FileName:=strFile, addtorecentfiles:=False
        Debug.Print "Opened: " & ActiveDocument.FullName
        
    '    ' Enable Auto macros
    '    WordBasic.DisableAutoMacros 0
        Application.ScreenUpdating = True
        System.Cursor = wdCursorNormal
        Set docWord = Nothing
    End Sub
    
    ' Run this second
    Public Sub RunMeSecond()
        Dim docWord As Word.Document
        Dim strFile As String
        
        Application.ScreenUpdating = False
        System.Cursor = wdCursorWait
        strFile = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator & CDbl(Now)
        
    '    ' Disable Auto macros
    '    WordBasic.DisableAutoMacros 1
        Set docWord = ActiveDocument
        With docWord
            .AttachedTemplate = MacroContainer.FullName
            .SaveAs FileName:=strFile, addtorecentfiles:=False
        End With
        Debug.Print "Saved: " & strFile
        Documents.Add Template:=MacroContainer.FullName
        docWord.Close
        Documents.Open FileName:=strFile, addtorecentfiles:=False
        Debug.Print "Opened: " & ActiveDocument.FullName
        
    '    ' Enable Auto macros
    '    WordBasic.DisableAutoMacros 0
        Application.ScreenUpdating = True
        System.Cursor = wdCursorNormal
        Set docWord = Nothing
    End Sub
    
    Viewing 0 reply threads
    Author
    Replies
    • #987144

      This is my 2nd attempt to post the code below.
      Don’t recall what I said in the original attempt.

      AFTER running the code I posted in the lead article in this thread, run the code below.
      You should see that auto macros are not disabled for that code because the code is using a different word application object.

      3rd party macros/add-ins/MSFT software could very well enable/disable automacros outside of the control of your own code.

      ' Run this third
      Public Sub RunMeThird()
          Dim appWord As Word.Application
          Dim docWord As Word.Document
          Dim strFile As String
          
          strFile = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator & CDbl(Now)
          Set appWord = New Word.Application
          With appWord
              .ScreenUpdating = False
              .Documents.Add
              .System.Cursor = wdCursorWait
              
          '    ' Disable Auto macros
          '    WordBasic.DisableAutoMacros 1
              Set docWord = .ActiveDocument
              With docWord
                  .AttachedTemplate = MacroContainer.FullName
                  .SaveAs FileName:=strFile, addtorecentfiles:=False
              End With
              Debug.Print "Saved: " & strFile
              .Documents.Add Template:=MacroContainer.FullName
              docWord.Close
              .Documents.Open FileName:=strFile, addtorecentfiles:=False
              Debug.Print "Opened: " & .ActiveDocument.FullName
              
          '    ' Enable Auto macros
          '    WordBasic.DisableAutoMacros 0
              .ScreenUpdating = True
              .System.Cursor = wdCursorNormal
              .Quit
          End With
          Set docWord = Nothing
          Set appWord = Nothing
      End Sub
      
    Viewing 0 reply threads
    Reply To: WordBasic.DisableAutoMacros (VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: