'How to insert fields in a Word document with PowerShell

I gang,

I'm looking for a way to insert fields in Word document with PowerShell.

I'm able to get the list of fields : [Enum]::GetNames([microsoft.office.interop.word.WdFieldType])

But unable to find the good sentence to use it... Do you have an example if I want to add a field in the footer ?

Tks

Base of code :

Word = New-Object -ComObject Word.Application;
$Word.Visible = $true;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Footer = $Section.Footers.Item(1);

$Footer.Range.Text = "Uername :";
#Line to add field wdFieldUserName

#$Word.Quit()

I know that the call in VB is :

Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, _ 
 Type:=wdFieldUserName)

Tried many formula to adapt it like :

$Section.fields.add($Section.Range,wdFieldUserName)

But no luck for the moment..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source