Search code examples
powershellobjectcomms-word

Powershell - How can I save a single page from a word document?


Need some help. Trying to build a tool that takes a 180 page document and reads the page number and saves that single page instance as a new file and an incremented file name. I've got the iteration and file process right but I have yet to find a way to select a single page from word. This may not be the best method but its working as all other attempts have failed. I even tried using the method suggested to adding page numbers to the footer on each page but it wont work with existing documents..only new documents. Here's what I have so far if anyone can pick and pull this apart of suggest somethings I'd appreciate it.

As a side note within that while loop namely; per page I want it to do really a save instance of the current page only. Right now its doing something different and just copying the entire thing to make sure the loop process was somewhat functional.

[int] $c = 1
[int] $total = 2

#$colFiles = gci("C:\xx\xx\Documents\xx\xx\*.docx") 
#$filebase = "FILENAME"


$objWord = New-Object -comobject Word.Application  
$objWord.Visible = $True 
$template = "C:\x\x\Documents\xx\xx.docx"  
$doc = $objWord.Documents.Open($template)

#$objDoc = $objWord.Documents.Open("C:\xx\xx\xx\xx\" + $objFile.Name) 
$filebase = "xx.docx"
$path = "C:\Users\username\Documents\vbscripting\"
$original = $path + $filebase

$baseFile = $filebase + "_" + $c + ".docx"


while($total -gt $c)
{
    $filename = $path + $filebase + "_" + $c + ".docx"
    $destination = "C:\xx\xx\Documents\xx\xx_"  + $c + ".docx"
    Copy-Item -Path "C:\xx\xx\Documents\xx\xx\xx.docx" -Destination $destination -Force
    ++$c
   Write $filename
}

Solution

  • One thing to keep in mind that a "page" is a relative term. Word is nothing more than a text editor, with cool formatting, so the file is basically one huge string set with things like /t for tabs and /n for new lines. So finding that specific page may pose some issues. As a point, if you were to open that same text document, in say, Notepad, you may not have the exact same number of words on that same page, or even the same content for that matter.

    Depending on the content of the file, you may be able to find start and end points, and pull out the needed section of content. You would read the file in line by line, looking for a particular identifier to, and pull that content out.

    Hope this helps.