2012-05-23 99 views
0

我正在尋找一種方法將頁眉和頁腳插入到Power Shell中生成的Microsoft Word文檔中。有沒有辦法做到這一點?如果是這樣,那麼完成這個任務所需的一些代碼的例子是什麼?對於具有用於SeekView可能值的列表使用Power Shell向Word文檔添加頁眉和頁腳

回答

2
# Create a new Word application COM object 
$Word = New-Object -ComObject Word.Application; 
# Make the Word application visible 
$Word.Visible = $true; 
# Add a new document to the application 
$Doc = $Word.Documents.Add(); 
# Get the first Section of the Document object 
$Section = $Doc.Sections.Item(1); 
# Get the header from the Section object 
$Header = $Section.Headers.Item(1); 
# Get the footer from the Section object 
$Footer = $Section.Footers.Item(1); 

# Set the text for the header and footer 
$Header.Range.Text = "Hey, I'm the header!"; 
$Footer.Range.Text = "Hey, I'm the footer!"; 

# Create a Table of Contents (ToC) 
$Toc = $Doc.TablesOfContents.Add($Section.Range); 
+0

出於好奇,你有章節列出的方式,能夠用於生成相同類型的Power Shell生成的Word文檔內的目錄? – John

+0

$ Toc = $ Doc.TablesOfContents.Add($ Section.Range); –

+0

令人敬畏的信息。如果有一個參考,我可以看看更多這一點?另外,我將如何在段落和標題中爲文本添加粗體?如何設置文本的水平位置,如中心,左側或右側? – John

3
$Document = "c:\temp\tralala.doc" # Must exist 

$Word = New-Object -Com Word.Application 
$Word.Visible = $true 
$ExistingDoc = $Word.Documents.Open($document) 
$Selection = $Word.Selection 
$ExistingDoc.ActiveWindow.ActivePane.View.SeekView = 1 
$Selection.TypeText("Here is my automated header") 
$ExistingDoc.ActiveWindow.ActivePane.View.SeekView = 4 
$Selection.TypeText("Here is my automated footer") 
$ExistingDoc.Save() 
$Word.Quit() 

,見here。 WdSeekView部分。

相關問題