Word Makros aus Powershell aufrufen
Hier ein Code-Schnipsel, um aus Powershell Word heraus zu starten und das Makro „Uwe“ auszuführen:
$Word = New-Object -Com Word.Application $Word.Visible = $true $oMissing = [System.Reflection.Missing]::Value $Document = $Word.Documents.Add($oMissing, $oMissing, $oMissing, $oMissing) $Word.Run("Uwe") |
Hier noch als kleine Erweiterung die Speicherung des Dokuments im docm
-Format mit Makros:
$Word = New-Object -Com Word.Application $Word.Visible = $true $oMissing = [System.Reflection.Missing]::Value $Document = $Word.Documents.Add($oMissing, $oMissing, $oMissing, $oMissing) # http://msdn.microsoft.com/en-us/library/bb238158%28v=office.12%29.aspx $formatDOCM = [ref] 13 $Document.SaveAs([ref] "C:\testdoc.docm",$formatDOCM) $Word.Run("Uwe") $a = $Word.Quit() |
Hinweis: Dieser Code wurde mit Word 2010 ausprobiert, Word 2007 beschwerte sich über die [ref] Konstrukte in den letzten Zeilen. Wenn man diese löscht, funktioniert es auch mit Word 2007.
Hinweis: Am 10.08.2017 angepasst, dank an Paula!