question

powerswitch-2610 avatar image
0 Votes"
powerswitch-2610 asked powerswitch-2610 edited

extract header or footer from word as a table (powershell)

Hi dear colleagues,

i would request your help to get the fields of the header or footer in proper format.
The code is here:

 $docPath = "C:\Users\Dummy\Desktop\"
 $outputfile = "C:\Users\Dummy\Desktop\header_out.csv"
 if(Test-Path -path $outputfile) { Remove-Item -path $outputfile }
    
 $all_docs = Get-ChildItem $docPath -filter "n.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);
     
  $header = $doc.Sections.Item(1).Headers.Item(1).Range.Text
    
     
  $item | Add-Member -type NoteProperty -name Name -value $document.FullName
  $item | Add-Member -type NoteProperty -name Header -value $doc.Sections.Item(1).Headers.Item(1).Range.Text
  $all_items += $item
  $doc.Close()
     
 }
 $word.Quit()
 Remove-Variable doc
 Remove-Variable word
 $all_items | Export-CSV $outputfile

but the csv output returns all the data with the title of fields unrelated with his values like this

Confidentiality Class External Confidentiality Label Document Type Page Public Non restricted Instruction 0 (1) Prepared By (Subject Responsibl
e) Approved By (Document Responsible) Checked Powersitch Powerswitch boss Monday Document Number Revision Date Reference Number 32232 PA2 2021
-06-29 WPH-1

the searched format would be (like the table of the header) :


Confidentiality Class External Confidentiality Label Document Type Page
Public Non restricted Instruction 1 (1)


Prepared By (Subject Responsible) Approved By (Document Responsible) Checked
Powersitch Powerswitch boss Monday


Document Number Revision Date Reference
Number 32232 PA2 2021-06-29 WPH-1



111820-capture.png


edit: added a screenshot

please, could anybody help to reach this?

Thanks & BR to all !!!!


windows-server-powershell
capture.png (33.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers