Results 1 to 2 of 2
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    WordBasic.DisableAutoMacros (VBA)

    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.

    <pre>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
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WordBasic.DisableAutoMacros (VBA)

    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.
    <pre>' 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
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •