Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Select-All from Word Doc

Tags:

powershell

I want to open, then select all of the text from a word document, not any of the properties, formatting, etc. Ihave searched this site and googled it to no end. Basically similar to opening a Word doc and pressing Ctrl-A and assigning the result to a variable.

$word = New-Object -ComObject Word.Application
$word.visible = $True
$wordfilepath = "\\symphony1\powershell\Phones\Phone.docx"
$doc = $word.Documents.Open($wordfilepath)
????
$selection" >> $textfilepath

Basically a newbie question, but can anyone help? Thanks.

like image 447
rstp Avatar asked Apr 27 '26 14:04

rstp


1 Answers

This will probably suit your needs. It creates a new word object, opens your existing file, and pulls the text from it.

$filePath = <your file here>
$doc = New-Object -com word.application
$fileToOpen = $doc.Documents.Open("$filePath")
$text = $fileToOpen.Range().text

Be forewarned that it will strip out even very basic formatting features such as new lines. Here's a nice list of other range members and properties that you may find helpful.

like image 64
Anthony Neace Avatar answered Apr 29 '26 12:04

Anthony Neace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!