locked
Retrieving Footer Information from Word Documents RRS feed

  • Question

  • My first PowerShell script...  I'm trying to create a csv file with a Word filename and it's associated footer.  I'm really close, but can't quite get it.  The script is using two approaches - one is the "out-file" command, but it doesn't format the file very well - especially if there are carriage returns in the footer.  The other approach is using "add-member" and "export-csv".  I like the formatting of this but I can't get each filename/footer on it's own line.  In my test directory, I have 5 documents.  The output file has 10 columns as the code is concatenating the each document information onto the same row.

    The code is below.  Any assistance is greatly appreciated!

    Thanks,
    Cindy

    $docPath = $args[0]
    $outputfile = "C:\PowerShellScripts\footer_out.csv"
    if(Test-Path -path $outputfile)
      { Remove-Item -path $outputfile }
     "filename, footer" >> $outputfile
    
    Write-Host "Processing Documents from:" $docPath 
    $all_docs = Get-ChildItem $docPath -filter "*.docx"
    $word = New-Object -comobject "Word.Application"
    $word.Visible = $False
    $all_items = @()
    $item = New-Object System.Object
    foreach ( $doc in $all_docs)
    {
     Write-Host "Processing :" $doc.FullName
     $doc = $word.Documents.Open($doc.FullName);
     
     $footer = $doc.Sections.Item(1).Footers.Item(1).Range.Text
     Write-Host "Footer : " $doc.Sections.Item(1).Footers.Item(1).Range.Text
     
     $item | Add-Member -type NoteProperty -name $doc.FullName -value $doc.FullName
     $item | Add-Member -type NoteProperty -name $doc.Sections.Item(1).Footers.Item
    (1).Range.Text -value $doc.Sections.Item(1).Footers.Item(1).Range.Text
     $item 
     $all_items += $item
     $doc.FullName + "," + $doc.Sections.Item(1).Footers.Item(1).Range.Text | out-File 
    $outputfile -append
    
     $doc.Close()
    }
    $word.Quit()
    Remove-Variable doc
    Remove-Variable word
    $all_items | Export-CSV "Footer.CSV"

     

     

     

    Wednesday, July 28, 2010 8:07 PM

Answers

  • The first issue is that "$item = New-Object System.Object" is outside of your foreach loop. This needs to be inside the loop because $all_items is an array of custom objects, and a new object needs to be created for each word document you are processing.

    When you run Add-Member, the -name should be a constant. This is where you are specifying the name of your property, so when you export to a CSV it will end up being the name of your column.

    Try this and see if it's closer to what you are looking for:

     

    $docPath = "c:\"
    $outputfile = "footer_out.csv"
    if(Test-Path -path $outputfile) { Remove-Item -path $outputfile }
    
    $all_docs = Get-ChildItem $docPath -filter "*.docx"
    $word = New-Object -comobject "Word.Application"
    $word.Visible = $False
    $all_items = @()
    
    foreach ( $document in $all_docs)
    {
    
     $item = New-Object System.Object
     $doc = $word.Documents.Open($document.FullName);
     
     $footer = $doc.Sections.Item(1).Footers.Item(1).Range.Text
    
     
     $item | Add-Member -type NoteProperty -name Name -value $document.FullName
     $item | Add-Member -type NoteProperty -name Footer -value $doc.Sections.Item(1).Footers.Item(1).Range.Text
     $all_items += $item
    
     $doc.Close()
     
    }
    $word.Quit()
    Remove-Variable doc
    Remove-Variable word
    $all_items | Export-CSV $outputfile

     

    • Proposed as answer by Marco Shaw Thursday, July 29, 2010 2:39 AM
    • Marked as answer by CavyPrincess Thursday, July 29, 2010 1:02 PM
    Wednesday, July 28, 2010 10:00 PM

All replies

  • The first issue is that "$item = New-Object System.Object" is outside of your foreach loop. This needs to be inside the loop because $all_items is an array of custom objects, and a new object needs to be created for each word document you are processing.

    When you run Add-Member, the -name should be a constant. This is where you are specifying the name of your property, so when you export to a CSV it will end up being the name of your column.

    Try this and see if it's closer to what you are looking for:

     

    $docPath = "c:\"
    $outputfile = "footer_out.csv"
    if(Test-Path -path $outputfile) { Remove-Item -path $outputfile }
    
    $all_docs = Get-ChildItem $docPath -filter "*.docx"
    $word = New-Object -comobject "Word.Application"
    $word.Visible = $False
    $all_items = @()
    
    foreach ( $document in $all_docs)
    {
    
     $item = New-Object System.Object
     $doc = $word.Documents.Open($document.FullName);
     
     $footer = $doc.Sections.Item(1).Footers.Item(1).Range.Text
    
     
     $item | Add-Member -type NoteProperty -name Name -value $document.FullName
     $item | Add-Member -type NoteProperty -name Footer -value $doc.Sections.Item(1).Footers.Item(1).Range.Text
     $all_items += $item
    
     $doc.Close()
     
    }
    $word.Quit()
    Remove-Variable doc
    Remove-Variable word
    $all_items | Export-CSV $outputfile

     

    • Proposed as answer by Marco Shaw Thursday, July 29, 2010 2:39 AM
    • Marked as answer by CavyPrincess Thursday, July 29, 2010 1:02 PM
    Wednesday, July 28, 2010 10:00 PM
  • Thank-you VERY much!  I was quickly able to write the script to get the footer, then spent hours trying to get it to format in the file.  I had initially thought the getting the footer was going to be the "hard part", not creating the file.

    Your script worked perfectly exactly as presented.  To understand where I went wrong, I took my original script and incorporated your changes.  I had been playing with the placement of the "New Object System.Object" statement, so it doesn't surprise me, I ended-up with it in the wrong place.  My biggest mistake that I could not figure-out on my own was the incorrect name parameter on the "Add-Member" command.  That was the light-bulb!  I now have a better understanding of this code.

    I appreciate your quick response and help!

    Cindy

    Thursday, July 29, 2010 1:01 PM
  • Jason,

    I've been trying to use this but it only returns the last line of the Footer,

    How can i get the Full Footer message?

    I'm trying to check footers for a particular phrase, and if it doesn't exist, Add it.

    Any Help Appriciated,
    Mark Nash
    Wednesday, November 8, 2017 1:14 PM
  • Please open anew question. This one was closed and answered 7 years ago.


    \_(ツ)_/

    Wednesday, November 8, 2017 3:51 PM