robertocm Posted October 6, 2022 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!
Danp2 Posted October 6, 2022 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) Latest Webdriver UDF Release Webdriver Wiki FAQs
robertocm Posted October 6, 2022 Author 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!
TheXman Posted October 6, 2022 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 Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
robertocm Posted October 6, 2022 Author 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
Solution TheXman Posted October 6, 2022 Solution 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 Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
TheXman Posted October 6, 2022 Posted October 6, 2022 You're welcome! CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
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