Wednesday, November 18, 2015

PowerShell & Microsoft Word 2016 : Opening existing Word Documents using PowerShell.

 

In the previous post, we have seem that how can we can create a new word document using the templates. We have seen how to add and create new documents, but, we hasn't seen, how to open existing documents yet!

To open existing documents, we use the Open( ) , method of the application class.

Let's create a path to my existing word document file :

$myFile = "C:\Users\aman.dhally\Documents\PolyCom-ThreeParty Conefrence.docx"

and then then we have to use the open( ) method. and in parentheses provide the name of the file which we want to be open.

$word.Application.Documents.open($myFile) | Out-Null

That's all :)

The complete code should be look like.

$word = New-Object -ComObject "Word.Application"

$word.Visible = $true

$myFile = "C:\Users\aman.dhally\Documents\PolyCom-ThreeParty Conefrence.docx"

$word.Application.Documents.open($myFile) | Out-Null

try it :)

Aman Dhally - Manya kaur

giphy

 

With Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

1 comment:

  1. Getting the following error:
    Exception calling "Open" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    At D:\Temp\Script\Doc-Create.ps1:3 char:33
    + $word.Application.Documents.open <<<< ($myFile) | Out-Null
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

    ReplyDelete