robertocm 31 Posted October 6, 2022 Share Posted October 6, 2022 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 post Share on other sites
Danp2 1,382 Posted October 6, 2022 Share Posted October 6, 2022 From here, it appears that you need to supply an additional parameter to indicate whether you are enabling or disabling the macros. Have you tried like this (untested)? $oWord.WordBasic.DisableAutoMacros(1) WebDriver UDF [GH&S] Latest version Wiki FAQs Link to post Share on other sites
robertocm 31 Posted October 6, 2022 Author Share Posted October 6, 2022 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 post Share on other sites
TheXman 637 Posted October 6, 2022 Share Posted October 6, 2022 (edited) 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 October 6, 2022 by TheXman CryptoNG UDF: Cryptography API: Next Genjq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON ProcessorXml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example ScriptAbout Me "Any fool can know. The point is to understand." -Albert Einstein Link to post Share on other sites
robertocm 31 Posted October 6, 2022 Author Share Posted October 6, 2022 (edited) Tested, but seems that then i cannot run a macro (tested reseting value to 1): expandcollapse popup#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 October 6, 2022 by robertocm Link to post Share on other sites
Solution TheXman 637 Posted October 6, 2022 Solution Share Posted October 6, 2022 (edited) 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 October 6, 2022 by TheXman CryptoNG UDF: Cryptography API: Next Genjq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON ProcessorXml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example ScriptAbout Me "Any fool can know. The point is to understand." -Albert Einstein Link to post Share on other sites
robertocm 31 Posted October 6, 2022 Author Share Posted October 6, 2022 Solved, Many Thanks! Link to post Share on other sites
TheXman 637 Posted October 6, 2022 Share Posted October 6, 2022 You're welcome! CryptoNG UDF: Cryptography API: Next Genjq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON ProcessorXml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example ScriptAbout Me "Any fool can know. The point is to understand." -Albert Einstein Link to post Share on other sites
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now