使用Power Shell将AUTOTEXT添加到MS Word文档 [英] Add AUTOTEXT to MS Word Document with Power Shell
问题描述
我正在一个项目中,需要在X的第1页中将AUTOTEXT条目添加到Power Shell生成的MS Word文档的页眉和页脚中.我尝试从遵循C#提取思想例子,但我似乎无法弄清楚如何使其发挥作用.我很好奇是否有人可以共享一些代码来帮助我.
I am working on a project where I need to add AUTOTEXT entries like the Page 1 of X listings to the header and footer of a Power Shell generated MS Word document. I have tried extracting ideas from the following C# examples, but I cannot seem to figure out how to make it work. I was curious if someone could share some code to help me with this.
推荐答案
起点.此函数将页码添加到作为参数传递的文档的页脚中(在单词2010上使用):
A starting point. This function add page number to the footer of document passed as parameter ( used on word 2010):
function Add-PageFooter ([string]$Document) {
add-type -AssemblyName "Microsoft.Office.Interop.Word"
set-variable -name wdAlignPageNumberCenter -value 1 -option constant
$fc1 = "Page"
$word = New-Object -comobject Word.Application
$Word.Visible = $True
#$Word.Visible = $False
$fc2 = [ref] "" -as [Type]
$OpenDoc = $Word.Documents.Open($Document)
$c = $OpenDoc.Sections.Item(1).Footers.Item(1).PageNumbers.Add($wdAlignPageNumberCenter)
$range1 = $openDoc.Sections.Item(1).Footers.Item(1).range
$field1 = $OpenDoc.Fields.Add($range1, -1, $fc2)
$field1.Code.Text = $fc1
$field1.Update
#$OpenDoc.Close()
}
另一种方法是创建Word Macro
并从powershell执行:
Another way is to create a Word Macro
and execute from powershell:
$wd = new-object -comobject word.application # create a com object interface (word application)
$wd.documents.open("C:\word\test.doc") # open doc
$wd.run("Macro01") # exec macro named macro01 that add custom footer and/or header
$wd.quit() # exit application
macro
必须保存在normal.dot
(对于2010年及更高版本,为normal.dotm
)上,以将其保存在所有打开的文档中.
The macro
must be saved on normal.dot
(normal.dotm
for 2010 and above) to have it in all open documents.
通过这种方式,您可以自定义Word文档中所需的内容,而不仅仅是在宏中记录页眉/页脚中文档中的操作.
In this way you can customize what you want in a word document and not just header/footer recording in a macro your actions in the docs.
这篇关于使用Power Shell将AUTOTEXT添加到MS Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!