Get paragraphs from a Word document via Powershell

Handling a Word document  (doc) could be a diffcult task for an automatization project. As you know i like using Powershell for automatization tasks so i decided to get a solution for the “get the sentences” from a document.

Of course we need a word application to be installed to the machine. So the first step is to open the doc:

$wd = New-Object -com word.application
$docu = $wd.Documents.Open(“d:\example.doc”)

If we would like the application to be visible we can use the visible property for this purpose: $wd.visible = $true  By default this property has the value: $false

And here we are. Let’s get all the characters from the doc:

$docu.Range().text

It has no structure at the moment. If we would like to handle the sentences in a structured way we can use:

$docu.Range().paragraphs | foreach {$_.range.text}

If there is a DropDown list in the document we can get the selected value for that in the next way:

$docu.FormFields.item(1).result

If we want to save the docu just use the save method: docu.save()

Closing the document without saving that: $docu.close($false)

And of course close the application then: $wd.quit()

 

i hope i could give a little help.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s