Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

powershell microsoft word add field code to footer 1

Status
Not open for further replies.

reesewilson

IS-IT--Management
Nov 30, 2010
4
US
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 is probably a terrible way to do that, but, the real question is, how do you add field codes to footers in Word documents using PowerShell?
 
Thanks, but, I definitely spent many hours on Google before posting here. I was able to add text, and even page numbers to the footers, but I can't find anything about adding field codes.
 

param ([string]$Document, [switch]$help)

function GetHelp() {


$HelpText = @"
SYNTAX:
Edit-Footer.ps1 -Document 'C:\Folder\Document.docx'
"@
$HelpText

}


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

#Variables used
set-variable -name wdAlignPageNumberCenter -value 1 -option constant

$Word = New-Object -comobject Word.Application
$Word.Visible = $True
#$Word.Visible = $False

$OpenDoc = $Word.Documents.Open($Document)
$c = $OpenDoc.Sections.Item(1).Footers.Item(1).PageNumbers.Add($wdAlignPageNumberCenter)

#$OpenDoc.Close()
}

if ($help) { GetHelp }

if ($Document) { Edit-Footer $Document }
 
This needs some cleanup, but it's late and I'm tired ;-) I'll come back to it tomorrow. Try this
Code:
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
    $e1 = [ref] $Word.WdFieldType.wdFieldEmpty -as [Type]
    $fc2 = [ref] $fc1 -as [Type]
    $pf1 = [ref] $true -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
    $OpenDoc.Fields.Add($range1, -1, $fc2)
    
    #$OpenDoc.Close()
}
 
OK, corrections. You have to add the field as an empty field, then add the code to that field. This code is a whole lot closer. Word (2010 in my case) doesn't like the field code itself, but at least does now recognize it as a field:
Code:
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()
}
 
Thanks, that is exactly what I was looking for. I can finish from here. Can you tell me where you found how to do that? Documentation somewhere perhaps?
 
A lot more Google searching and experimenting.
Word Field object documentation: Several Microsoft "Hey, Scripting Guy!" blog entries
Looking at VB/VBA code samples and trying to translate them to PowerShell.

Some searches included
word com object powershell
word com object footer add program
word com object fields.add.range
powershell word com object range footer

There was no one spot that had the exact answer, it was basically pulling bits of information from several different places. I can't find it now of course, but a blog entry I found this morning was what led to the realization the field had to be added and then modified to include the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top