Sunday, June 24, 2018

How do you open a Word document using Power Shell?

You need to create a new COM object, namely a Word.Application in Power Shell.

After creating the new object you call the Documents to open providing a file name for the WORD document. If the intellisense is working you should be able to open the document following the drop-down menus shown here:

Creating a New-Object



$word= New-Object -ComObject Word.Application

$word.Documents:



There are three 'open' options. Open2002 did not work.
$word.Documents.Open


$fileName='C:\Users\Owner\Desktop\June5-7\Schedule.docx '



$word.Documents.Open($fileName)

This when run opens a large XML file in the console as well as the Word document 'Schedule.docx'




I have also encountered problems with the Power Shell interface. I suddenly stopped providing the intellisense drop-downs (for example, it did not provide the Open option but after typing in Open, it did provide intellisense again. This was sporadic).

Also opening was not clean as it also provided an XML with binary of the word document.

The Power shell version used on Windows 10 Pro(x64) was:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  112  
 

Also all of a sudden you get a request for Outlook login(Surprise, surprise)




No comments: