none
Adding FileName to Footer of existing Word Doc RRS feed

  • Question

  • Hello all! I am working on a PS script to allow me to search a directory for all .doc and .docx files and then add the file name and path to the footer. I have it to where I can add the page numbers but I can't figure what I need to do to adjust it to add the file name instead. Any help is greatly appreciated. Here is what I have so far:

    $path = "C:\FooterTest" 
    Add-Type -AssemblyName Microsoft.Office.Interop.Word 
    $wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
    $word = New-Object -ComObject Word.Application
    $word.Visible = $true # Remove this line later to keep the application invisible; when testing though, better to work with a visible application 
    
    set-variable -name wdFieldFileName -value 1 -option constant
    
    foreach ($wd in $wordFiles) 
    {
         $doc = $word.documents.open($wd.fullname)     
         if ($doc -eq $null) {continue}
         $c = $doc.Sections.Item(1).Footers.Item(1).PageNumbers.Add($wdFieldFileName)
         #$range1 = $doc.Sections.Item(1).Footers.Item(1).range
    
         $doc.Save()
         $doc.Close() 
         $doc = $null
    }
    $word.Quit()


    • Edited by JMcDade87 Thursday, September 20, 2012 3:29 PM Typos
    • Moved by Bill_Stewart Monday, November 13, 2017 3:40 PM Move to more appropriate forum
    Thursday, September 20, 2012 3:28 PM

Answers

  • I've finally figured it out! Here's what I've got:

    $path = "C:\FooterTest" 
    Add-Type -AssemblyName Microsoft.Office.Interop.Word 
    $wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
    
    $word = New-Object -ComObject Word.Application
    #$word.Visible = $true # Remove this line later to keep the application invisible; when 
    
    testing though, better to work with a visible application 
    
    set-variable -name wdFieldFileName -value 1 -option constant
    
    foreach ($wd in $wordFiles) 
    {
         $doc = $word.documents.open($wd.fullname)     
         if ($doc -eq $null) {continue}
    
         $section = $doc.Sections.Item(1)
         $footer = $section.Footers.Item(1)
         $footer.Range.Text = $wd.fullname
         
         $doc.Save()
         $doc.Close() 
         $doc = $null
    }
    $word.Quit()

    • Proposed as answer by thepip3r Thursday, September 20, 2012 8:02 PM
    • Marked as answer by IamMred Thursday, September 20, 2012 10:37 PM
    Thursday, September 20, 2012 5:28 PM

All replies

  • I've finally figured it out! Here's what I've got:

    $path = "C:\FooterTest" 
    Add-Type -AssemblyName Microsoft.Office.Interop.Word 
    $wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
    
    $word = New-Object -ComObject Word.Application
    #$word.Visible = $true # Remove this line later to keep the application invisible; when 
    
    testing though, better to work with a visible application 
    
    set-variable -name wdFieldFileName -value 1 -option constant
    
    foreach ($wd in $wordFiles) 
    {
         $doc = $word.documents.open($wd.fullname)     
         if ($doc -eq $null) {continue}
    
         $section = $doc.Sections.Item(1)
         $footer = $section.Footers.Item(1)
         $footer.Range.Text = $wd.fullname
         
         $doc.Save()
         $doc.Close() 
         $doc = $null
    }
    $word.Quit()

    • Proposed as answer by thepip3r Thursday, September 20, 2012 8:02 PM
    • Marked as answer by IamMred Thursday, September 20, 2012 10:37 PM
    Thursday, September 20, 2012 5:28 PM
  • Hi,

    I'm trying to write a script that will check a word document's footer for a set of phrases.
    If Not found, it will add the correct phrase,
    If Found and incorrect it will replace it with the correct phrase
    If Found and correct it will ignore.

    I am currently struggling to retrieve the full footer for any Document, and Insert Text into the correct place.

    My Code so far:
    $Word = New-Object -Com Word.Application
    $Word.Visible = $false
    
    $File = "C:\Test.docx" # Must exist
    $Doc = $Word.Documents.Open($file)
    
    $Selection = $Word.Selection
    
    $Doc.ActiveWindow.ActivePane.View.SeekView = 4
    
    # Commented out Code for writing to the footer
    #$Selection.TypeText("Here is my")
    #$Selection.TypeParagraph()
    #$Selection.TypeText(" automated footer")
    
    Write-Output "Footer: "$Doc.Sections.Item(1).Footers.Item(1).Range.Text()
    
    $Doc.Save()
    $Word.Quit()
    When run, the code gets all of the new lines that were added within Word. But it doesn't grab any of the new lines or paragraphs added through Powershell.

    Also the way i'm currently trying to write to the file adds the text to the start of the footer.

    I need the full footer so that i can check it for the phrase, and then i want to be able to put a new line and then the correct phrase at the End of the Footer.

    Any help really appreciated
    Mark
    • Merged by jrv Thursday, November 9, 2017 7:22 PM duplicate
    Thursday, November 9, 2017 3:13 PM
  • You were given the answer above. It hasn't changed.

    I recommend learning Word programming from many articles and tutorials.  Also posting the Word developers forum will get you answers faster.


    \_(ツ)_/

    Thursday, November 9, 2017 7:23 PM
  • As Mentioned in my Question, it currently doesn't get the whole footer,

    Lines of the footer are missing and browsing articles and tutorials have not helped as none solve the issue i'm having.

    If i had more points i'd be able to attach screen shots to show what i am seeing.

    Essentially the footer looks something like :
    "This is the start of the footer

    This is the End of the Footer"

    When using my code above it doesn't take the line break, and only retrieves "This is the End of the Footer"

    if anyone could help me out that would be great

    Monday, November 13, 2017 11:22 AM
  • Ok building on my Issue.

    When looking at

    $Doc.Sections.Item(1).Footers.Item(1).Range

    I Can see the full footer in the Text field, (All on one line with no line breaks)

    However, when i Access it by using

    $Doc.Sections.Item(1).Footers.Item(1).Range.Text()

    It only passes me the last section after all of the line breaks


    Is there a different way i can access that field to get the full footer?

    Monday, November 13, 2017 12:30 PM
  • The footer is a collection of objects.  It does not convert to text.


    \_(ツ)_/

    Monday, November 13, 2017 6:38 PM
  • OK so the footer text field is just not converting to text properly in the powershell console.

    When you run a search against the Footer to search for a select word that isn't showing, it does correctly identify if its in the Footer


    Wednesday, November 15, 2017 2:20 PM