Word页眉和页脚中的Powershell更新字段 [英] Powershell Update Fields in Header and Footer in Word
问题描述
我正在尝试使Powershell脚本正常工作,该脚本需要更新所有页面的页眉和页脚中的所有字段.但是,即使经过数小时的研究,我仍然无法更新正文中的各个字段.当然,这确实非常有用,并且一切都在使用,我也正在使用它,但是我还想更新标题中的字段.
I'm trying to get a Powershell Script working that needs to update all fields I have in Header and Footers of all pages. But even after hours of research, I don't get any farther than being able to update the fields in the main text. Sure, that is really useful and everything and I am using it as well, but I would also like to update the fields in the headers.
恐怕除了向您展示我拥有的可更新文档中所有字段的摘录之外,我无济于事.所以就这样了.
I'm afraid I can't be of much help other than showing you the snippet I have that updates all my fields in the document. So here goes that.
objWord = New-Object -ComObject word.application
$objWord.ActiveDocument.Fields.Update()
所以,请帮忙.
推荐答案
从今天早上开始我一直在四处张望,找不到适合我的方法. (对OP的类似请求)
I have been looking around since this morning and cannot find a approach that works for me. (Similar request to OP)
我已经检查了Adil的链接,但是并没有更新所有文档.
I have checked Adil's link however it did not update all of the document.
我需要更新添加到文档中的外部链接(对象).正文,页眉和页脚需要更新,但是由于许多模板使用多个部分,所以我遇到了问题.
I needed to update external links (Objects) that I added to documents. Body, Headers and Footers need to be updated, however since many of the templates use multiple sections I was having issues.
我相信在Sections循环中会浪费一些时间,但是它可以确保整个文档都得到更新.
I believe there is some waste during the Sections loop however it ensures that the entire document is updated.
这是我的解决方案:
# Open Word Instance (Use True for Debugging)
$Word = New-Object -ComObject "Word.Application"
$Word.Visible = $True
# Open Template for Editing
$Doc = $Word.Documents.Open($file.FullName)
# Update Main Content
$Word.ActiveDocument.Fields.Update()
# Iterate through Sections
foreach ($Section in $Doc.Sections)
{
# Update Header
$Header = $Section.Headers.Item(1)
$Header.Range.Fields.Update()
# Update Footer
$Footer = $Section.Footers.Item(1)
$Footer.Range.Fields.Update()
}
# Save and Close Template
$Doc.Save()
$Doc.Close()
# Exit Word Instance
$Word.Quit()
这篇关于Word页眉和页脚中的Powershell更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!