简体   繁体   English

PowerShell将域代码添加到MS Word页脚

[英]PowerShell add field codes to ms word footer

I have field codes set up in some documents which will display the current date when it is printed, but be invisible the rest of the time, as far as I know. 我在某些文档中设置了域代码,据我所知,它将在打印时显示当前日期,但在其余时间中不可见。 I now need to apply this footer to hundreds of documents. 我现在需要将此页脚应用于数百个文档。 It seems like this should be possible to do with PowerShell, but I don't really know how to use it, and can't find a good reference/documentation (So far I have gotten it to open a document, replace text, and close the document). 使用PowerShell似乎应该可以做到这一点,但是我真的不知道如何使用它,也找不到很好的参考/文档(到目前为止,我已经获得了它来打开文档,替换文本和关闭文档)。 I would like the following Field Code to be on the right side of every footer section: 我希望以下页脚代码位于每个页脚部分的右侧:

{ IF{PRINTDATE \\@ "M/d/yyyy h:mm"}={DATE \\@ "M/d/yyyy h:mm"} "UNCONTROLLED COPY AS OF { DATE \\@"M/d/yyyy"}" " " {IF {PRINTDATE \\ @“ M / d / yyyy h:mm”} = {DATE \\ @“ M / d / yyyy h:mm”}“截至{DATE \\ @” M / d / yyyy“}的副本不受控制“”

I know that is probably a terrible way to do that, but, the real question is, how do you add field codes to footers in Word 2007 documents using PowerShell? 我知道这可能是一种糟糕的方法,但是,真正的问题是,如何使用PowerShell将域代码添加到Word 2007文档的页脚中?

Thanks to crobin1 at tek-tips for the answer to this one. 感谢tek-tips的crobin1为这一问题的答案。 He said he referenced http://msdn.microsoft.com/en-us/library/bb258930%28v=office.12%29.aspx and a bunch of "Hey, Scripting Guy!" 他说他引用了http://msdn.microsoft.com/en-us/library/bb258930%28v=office.12%29.aspx和一堆“嘿,脚本专家!” blog entries 博客条目
Here is an example script that pretty much does what I wanted. 这是一个示例脚本,几乎可以完成我想要的。

function Edit-Footer ([string]$Document) { 函数Edit-Footer([string] $ Document){

 add-type -AssemblyName "Microsoft.Office.Interop.Word" #Variables used set-variable -name wdAlignPageNumberCenter -value 1 

-option constant 选项常数

 $fc1 = @" IF {PRINTDATE \\@ "M/d/yyyy h:mm"}={DATE \\@ "M/d/yyyy h:mm"} "UNCONTROLLED COPY AS OF {DATE \\@ "M/d/yyyy"}" " " "@ $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() } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM