Archive
Archive for December, 2015
How to automate removing MS-Office VBA project protection for multiple files
2015/12/01
Leave a comment
- Problem:
- Need VBE extensibility to implement some tools and practices of the modern SDLC.
- Software consists of not only 1000s of Word templates which are anything but DRY, but also highly protected even during what should be the SDLC
- Not solvable by VBA automaton for security reasons:
- VBE password protection (OK, there is Sendkeys, but that is considered harmful).
- digitally signed.
- Developer tab read-only protection: this one is not covered here, since it can be dealt with through regular VBA automation.
- Not solvable by VBA automaton for security reasons:
- Workaround: PowerShell for starters:
- Get Unlock-OfficeMacro.ps1 – including the addition in the comments.
- Wrap the downloaded script like so:
Get-ChildItem -Include *.do?m* -Exclude *_unlocked* -Path "G:\imf\word templates\Quarterly Releases_unprotected_ps" -Recurse |` foreach{ $_.IsReadOnly = $false $output_filename = $_.Directory.ToString() + "\" + $_.BaseName + '_unlocked' + $_.Extension .\Unlock-OfficeMacro.ps1 $_.FullName $output_filename } Exit
- NextProblem: The script removes the warning dialogues on opening the altered MS-Word files remain. This still hampers automation.
- Next workaround: this script automates the GUI:
- “OK”’ing the warning dialogue: “The project file ‘C:\Users\tplagwitz\AppData\Roaming\Microsoft\Templates\documaker.dotm’ contains invalid key ‘DPx’.–Continue Loading Project?”
- making minor changes and saving the file (this also bypasses the "discard certificate" warning, if the file was also signed (as is my case).
-
Prerequisites: none, other than putting your word files in a folder the script (which the script will prompt you for, and for an (optional) substring, to filter file names) .
- Limitations:
- I used to have also have, per module in the VBA project, warnings: “Microsoft Visual Basic for Applications Unexpected error (40230) ” and try to bypass these also, but since I cannot replicate the warnings, this remains untested.
- A superior approach (enabling round tripping) would be to attempt to automate entering the password, but the traditional SendKeys approach is unreliable, and newer approaches (using SendMessage from the the Win32 API or bypassing the intended negative effects of password protection, via an in-memory substitution).
- I used to have also have, per module in the VBA project, warnings: “Microsoft Visual Basic for Applications Unexpected error (40230) ” and try to bypass these also, but since I cannot replicate the warnings, this remains untested.
- And here is the AutoIt script:
- “OK”’ing the warning dialogue: “The project file ‘C:\Users\tplagwitz\AppData\Roaming\Microsoft\Templates\documaker.dotm’ contains invalid key ‘DPx’.–Continue Loading Project?”
include <Array.au3> #include <debug.au3> #include <File.au3> #include <log4a.au3> Opt("WinTitleMatchMode", 2) Opt("MustDeclareVars", 1) Dim $file, $runpath, $iPID, $i, $folderpath, $pattern, $files, $filepath, $files, $ret, $oAppl, $oDoc, $sFilter _log4a_SetEnable() _log4a_SetOutput($LOG4A_OUTPUT_BOTH) $pattern = InputBox("File Pattern?", "Enter file pattern, beyond (before) *.do?m (= Files with macros), that files have to match.", "_unlocked") $folderpath = InputBox("Where?", "Enter folder to find files in...") $sFilter = "*" & $pattern & "*.do?m|~*,Backup*" If Not (FileExists($folderpath) And StringInStr(FileGetAttrib($folderpath), "D")) Then MsgBox(1, "Error", " The path you entered does Not seem To exist Or is Not a folder. Exiting....") Exit Else $files = _FileListToArrayRec($folderpath, $sFilter, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_RELPATH) For $i = 1 To UBound($files) - 1 $file = $files[$i] If (StringRight($folderpath, 1) <> "\") Then $folderpath = $folderpath & "\" $filepath = $folderpath & $file Local $iPID = Run('"C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE" /q /a /m "' & $filepath & '"', "", @SW_SHOWMAXIMIZED) $ret = WinActivate("- Word", "") $ret = WinWaitActive("- Word", "", 5) If ($ret = 0) Then _log4a_debug("cannot load: " & $filepath & @TAB & @ScriptLineNumber & @CRLF) $ret = ProcessClose($iPID) ContinueLoop Else _log4a_debug("processing: " & $filepath & @TAB & @ScriptLineNumber & @CRLF) EndIf If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) Send("{SHIFTDOWN}{SHIFTUP}") Sleep(100) Send("!{f11}") $ret = WinActivate("Microsoft Visual Basic for Applications", "invalid key") If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) $ret = WinWaitActive("Microsoft Visual Basic for Applications", "invalid key", 5) If ($ret = 0) Then _log4a_debug("nothing to do with invalid key, will close word and continue next file: " & @ScriptLineNumber & @CRLF) $ret = ProcessClose($iPID) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) ContinueLoop EndIf If $ret <> 0 Then $ret = ControlClick($ret, "", "Button1") If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) Else If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) EndIf Sleep(1000) $ret = 0 Sleep(5000) While (0 <> WinActivate("Microsoft Visual Basic for Applications", "Unexpected error (40230)")) $ret = ControlClick("[CLASS:#32770 If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) WEnd Sleep(3000) $ret = WinActivate("Microsoft Visual Basic for Applications", "") $ret = WinWaitActive("Microsoft Visual Basic for Applications", "", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) If ($ret = 0) Then WinActivate("Microsoft Visual Basic for Applications", "") $ret = WinWaitActive("Microsoft Visual Basic for Applications", "", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) $ret = ControlSend("Microsoft Visual Basic for Applications", "", "VbaWindow1", "'dummy" & @CRLF) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) Sleep(1000) Send("!q") Sleep(1000) Send("!{f4}") Sleep(1000) $ret = WinWaitActive("Microsoft Word", "", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) If ($ret = 0) Then $ret = WinActivate("Microsoft Word", "") If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) $ret = WinWaitActive("Microsoft Word", "", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) If ($ret <> 0) Then Send("!s") Sleep(1000) $ret = WinWaitActive("Microsoft Word", "discarded", 5) If ($ret = 0) Then _log4a_debug("the certificate dialogue is not up yet", True) $ret = WinWaitActive("Microsoft Word", "", 5) If ($ret <> 0) Then _log4a_debug("failure with !s: " & @ScriptLineNumber & @CRLF) Send("{Enter}") EndIf EndIf Sleep(1000) Else If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) EndIf $ret = WinWaitActive("Microsoft Word", "discarded", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) $ret = WinActivate("Microsoft Word", "discarded") If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) $ret = WinWaitActive("Microsoft Word", "discarded", 5) If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) If ($ret <> 0) Then $ret = ControlClick("Microsoft Word", "", "Button1") If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) If $ret = 0 Then Send("!d") Else If ($ret = 0) Then _log4a_debug("failure on: " & @ScriptLineNumber & @CRLF) EndIf Sleep(1000) Send("{BACKSPACE}") Sleep(1000) Send("^s") Sleep(3000) ProcessClose($iPID) Sleep(1000) Next Sleep(1000) EndIf
Categories: audience-is-IT-staff, e-infrastructure, service-is-programming, sourcecode
ms-office, ms-powershell, openxml, protection, security, VBA, vbe