PowerShell add field codes to ms word footer

前端 未结 1 1028
暗喜
暗喜 2021-01-26 22:30

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

相关标签:
1条回答
  • 2021-01-26 22:38

    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 讨论(0)
提交回复
热议问题