开发者

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). 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"}" " "

I know that i开发者_StackOverflows 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?


Thanks to crobin1 at tek-tips for the answer to this one. He said he referenced http://msdn.microsoft.com/en-us/library/bb258930%28v=office.12%29.aspx and a bunch of "Hey, Scripting Guy!" blog entries
Here is an example script that pretty much does what I wanted.

function 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() }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜