Jump to content

Word UDF: WordBasic.DisableAutoMacros


Go to solution Solved by TheXman,

Recommended Posts

I have a word document with this code in standard module:

'Auto Macros
'https://learn.microsoft.com/en-us/office/vba/word/concepts/customizing-word/auto-macros
'Macro name  When it runs
'AutoExec    When you start Word or load a global template
'AutoNew     Each time you create a new document
'AutoOpen    Each time you open an existing document
'AutoClose   Each time you close a document
'AutoExit    When you exit Word or unload a global template
Sub AutoOpen() 'will be triggered by opening
MsgBox "AutoOpen"
End Sub

Trying to disable when opening from AutoIt script, but not working:

Global $oWord, $oDoc
;Create application object
$oWord = _Word_Create(False)

$oWord.WordBasic.DisableAutoMacros

$oDoc = _Word_DocOpen($oWord, $sFileName, Default, Default, True)

;...

Is it possible?

Many Thanks!

Link to comment
Share on other sites

Tested but then get error in the line:

->    We intercepted a COM Error !
->    err.number is:    80020006
->    err.source:   
->    err.windescription:   Nombre desconocido.
->    err.scriptline is:    53
->    We intercepted a COM Error !
->    err.number is:    80020003
->    err.source:   
->    err.windescription:   No se ha encontrado el miembro.
->    err.scriptline is:    174

Thanks!

Link to comment
Share on other sites

Try:

Enum $msoAutomationSecurityLow = 1, $msoAutomationSecurityByUI, $msoAutomationSecurityForceDisable
.
.
.
$oWord.AutomationSecurity = $msoAutomationSecurityForceDisable
OR
$oWord.AutomationSecurity = 3

You can read about the property and the enumeration values using the links below:

https://learn.microsoft.com/en-us/office/vba/api/word.application.automationsecurity

https://learn.microsoft.com/en-us/office/vba/api/office.msoautomationsecurity

 

Edited by TheXman
Link to comment
Share on other sites

Tested, but seems that then i cannot run a macro (tested reseting value to 1):

#include <WinAPIFiles.au3>
#include <Word.au3>

Opt("MustDeclareVars", 1)
Opt("TrayIconDebug", 1)

#===== CONFIG =====
Global $sAnswer, $sFolder, $sFolderPDF, $sFileName, $sFra, $bFileOpen
If $CmdLine[0] = 2 Then ;there are 2 parameters passed to the executable
    $sAnswer = $CmdLine[1]
    $sFolderPDF = $CmdLine[2]
Else
    $sFolderPDF = @DesktopDir & "\"
    If $CmdLine[0] = 1 Then
        $sAnswer = $CmdLine[1]
    Else
        $sAnswer = InputBox("Invoice", "Some string to pass:", "", "")
    EndIf
EndIf
If $sAnswer = "" Then Exit
$sFra = StringFormat("%09i", $sAnswer)
$sFolder = @ScriptDir & "\"
$sFileName = $sFolder & "test_invoice.docm"

#===== ERROR =====
Global $oMyError = ObjEvent("AutoIt.Error", "ErrFunc") ;Install a custom error handler
Global $iEventError ; to be checked to know if com error occurs. Must be reset after handling.

#===== CHECK ACCESO A CARPETA =====
;Introducir pausa: conexión de rede pode tardar en restaurarse cando equipo estaba suspendido
For $i = 0 To 5
    If FileExists($sFolder) Then
        ExitLoop
    Else
        Sleep(1000)
    EndIf
Next

#===== WORD =====
Global $oWord, $oDoc
;Create application object
$oWord = _Word_Create(False)
;~ $oWord.AutomationSecurity = 3 ;msoAutomationSecurityForceDisable
;~ ;$oWord.WordBasic.DisableAutoMacros(1)

$bFileOpen = _WinAPI_FileInUse($sFileName)
If $bFileOpen = 0 Then
    $oDoc = _Word_DocOpen($oWord, $sFileName, Default, Default, True)
    ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocPrint Example", "Error opening " & $sDocument & @CRLF & "@error = " & @error & ", @extended = " & @extended)

;~  $oWord.AutomationSecurity = 1 ;msoAutomationSecurityLow
    $oWord.Run("MyTestSub", $sFra)

    _Word_DocClose($oDoc)
    ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocClose Example", "Error closing document '.\Extras\Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Word_Quit($oWord)

    While _WinAPI_FileInUse($sFileName)
        Sleep(1000)
    WEnd
Else
    Consolewrite("Error Locked file: " & $sFileName & @CRLF)
EndIf

;word com error trapping possible bug in udf
;https://www.autoitscript.com/forum/topic/184633-word-com-error-trapping-possible-bug-in-udf/
;water
;"Unknown name" is no error. It's an information that is handled by the UDF.
Func ErrFunc()
    Local $HexNumber = Hex($oMyError.number, 8)
;~  MsgBox(0, "", "We intercepted a COM Error !" & @CRLF & _
;~      "Number is: " & $HexNumber & @CRLF & _
;~      "WinDescription is: " & $oMyError.windescription)
    ConsoleWrite("->    We intercepted a COM Error !" & @CRLF & _
        "->    err.number is: " & @TAB & $HexNumber & @CRLF & _
        "->    err.source: " & @TAB & $oMyError.source & @CRLF & _
        "->    err.windescription: " & @TAB & $oMyError.windescription & _
        "->    err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF)

    $iEventError = 1 ; Use to check when a COM Error occurs
EndFunc   ;==>ErrFunc

Thanks!

test_invoice.docm

Edited by robertocm
Link to comment
Share on other sites

  • Solution

I misunderstood what you wanted because your initial example did not show you trying to execute a subsequent macro.  I thought you wanted to disable all macros.  You are wanting to just suppress the "AutoOpen", but leave the ability to execute other macros.

To suppress just the "AutoOpen" macro, you can hold the SHIFT key down while opening the file.  This works for me:

Send("{SHIFTDOWN}")
_Word_DocOpen($oWord, $sFileName)
Send("{SHIFTUP}")

 

Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...