Controlling MS Word via Powershell


MS Word can be automated using Powershell. This has the advantage that „normally“ processable office documents can be generated completely automatically and one can, for example, generate Word documents automatically from a MySQL database without having to deal with VBScript.

Create an Office template for an scientific paper.

The following script will produce a structured template for a scientific paper (in this case with German Headings but this can changed easily).

$Word = New-Object -ComObject Word.Application
$Word.Visible = $True
$Document = $Word.Documents.Add()
$selection=$word.Selection
#https://docs.microsoft.com/en-us/office/vba/api/Word.WdBuiltinStyle enthält nicht alle. Ggf. graphische Oberfläche nutzen und mit dem Macrorecorder schauen.
$selection.Style=-63
$selection.TypeText("Abschlussdokumentation")
$selection.TypeParagraph()
$selection.Style=-75
$selection.TypeText("Author")
$selection.TypeParagraph()
#https://docs.microsoft.com/de-de/office/vba/api/word.wdbreaktype
$selection.InsertBreak(7)
$selection.InsertBreak(2)

$selection.Style=-267
$selection.TypeText("Metainformation")
$selection.TypeParagraph()
$selection.Style=-262
#https://unicodemap.org/
$selection.TypeText(0x00C4+"nderungen")
$selection.TypeParagraph()
$selection.Style=-262
$selection.TypeText("Freigaben")
$selection.TypeParagraph()
$selection.InsertBreak(7)


$selection.Style=-267
$selection.TypeText("Vorwort")
$selection.TypeParagraph()
$selection.InsertBreak(7)

$selection.Style=-267
$selection.TypeText("Abstract")
$selection.TypeParagraph()
$selection.InsertBreak(7)
$selection.InsertBreak(2)

$selection.Style=-267
$selection.TypeText("Inhalt")
$selection.TypeParagraph()
$rangeInhalt = $Selection.Range
$selection.InsertBreak(7)

$selection.Style=-267
$selection.TypeText("Abk"+[char]0x00FC+"rzungen")
$selection.TypeParagraph()
$rangeAbkuerzungen = $Selection.Range
$selection.InsertBreak(7)

$selection.Style=-267
$selection.TypeText("Abbildungen")
$selection.TypeParagraph()
$rangeAbbildungen = $Selection.Range
$selection.InsertBreak(7)

$selection.Style=-267
$selection.TypeText("Tabellen")
$selection.TypeParagraph()
$rangeTabellen = $Selection.Range
$selection.InsertBreak(7)
$selection.InsertBreak(2)

$selection.Style=-2
$selection.TypeText("Einleitung")
$selection.TypeParagraph()
$selection.Style=-1
$selection.TypeText("Bla bla blub")
$selection.TypeParagraph()
$selection.Style=-3
$selection.TypeText([char]0x00DC+"berschrift")
$selection.TypeParagraph()
$selection.Style=-1
$selection.TypeText("Bla bla blub")
$selection.TypeParagraph()
$selection.InsertBreak(7)

$selection.Style=-2
$selection.TypeText("Hauptteil")
$selection.TypeParagraph()

$selection.InsertBreak(7)

$selection.Style=-2
$selection.TypeText("Zusammenfassung")
$selection.TypeParagraph()
$selection.InsertBreak(7)


$selection.Style=-2
$selection.TypeText("Literatur")
$selection.TypeParagraph()
$selection.InsertBreak(7)

$selection.Style=-2
$selection.TypeText("Rechtsnormen")
$selection.TypeParagraph()
$selection.InsertBreak(7)

$selection.Style=-2
$selection.TypeText("Stichworte")
$selection.TypeParagraph()
$selection.InsertBreak(7)

$selection.Style=-2
$selection.TypeText("Anhang")
$selection.TypeParagraph()

$Document.TablesOfContents.Format = 1
$toc = $Document.TablesOfContents.Add($rangeInhalt)
$Document.TablesOfContents(1).Update()
$Selection.TypeParagraph()

$section=$Document.Sections(1)
$header=$section.headers.Item(1)
$header.LinkToPrevious = $False
$header.Range.Text ="Header"

$footer = $section.Footers.Item(1)
$footer.Range.InsertAlignmentTab($alignmentTab::wdRight)
$footer.LinkToPrevious = $False
$footer.Range.Text =""

$section2=$Document.Sections(2)
$header2=$section2.headers.Item(1)
$header2.LinkToPrevious = $False
$header2.Range.Text ="Kopf"

$footer2 = $section2.Footers.Item(1)
$footer2.LinkToPrevious = $False

$section3=$Document.Sections(3)
$section4=$Document.Sections(4)
$header4=$section4.headers.Item(1)
$header4.LinkToPrevious = $False
$header4.Range.Text ="Inhalt"

$footer3 = $section3.Footers.Item(1)
$footer3.LinkToPrevious = $False
$footer3.PageNumbers.Add()

$Report = "c:\Tutorial\Report.docx"
$Document.SaveAs([ref]$Report,[ref]$SaveFormat::wdFormatDocument)


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden.

WordPress Cookie Plugin von Real Cookie Banner