PowerShell for Windows Admins

Oct 15 2012   11:21AM GMT

PowerShell 3 and Word



Posted by: Richard Siddaway
Tags:
PowerShell 3

 

This is a common scenario

$word = New-Object -ComObject "Word.application"            
$word.visible = $true            
$doc = $word.Documents.Add()            
$doc.Activate()            
            
$word.Selection.Font.Name = "Cambria"            
$word.Selection.Font.Size = "20"            
$word.Selection.TypeText("PowerShell")            
$word.Selection.TypeParagraph()            
            
$word.Selection.Font.Name = "Calibri"            
$word.Selection.Font.Size = "12"            
$word.Selection.TypeText("The best scripting language in the world!")            
$word.Selection.TypeParagraph()            
            
$file = "c:\scripts\office\test1.doc"            
$doc.SaveAs([REF]$file)            
            
$Word.Quit()

Create a new Word document – put some text into it and save it with a given file name.  I’ve used it successfully to create server documentation.

Unfortunately with PowerShell v3 it fails with this message

 

Exception calling "SaveAs" with "1" argument(s): "This is not a valid file name.

Try one or more of the following:

* Check the path to make sure it was typed correctly.

* Select a file from the list of files and folders."

At line:17 char:1

+ $doc.SaveAs([REF]$file)

+ ~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : COMException

It appears not to like the [ref] but if you leave it out you get this

Argument: ’1′ should be a System.Management.Automation.PSReference. Use [ref].

At line:18 char:1

+ $doc.SaveAs($file)

+ ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodException

    + FullyQualifiedErrorId : NonRefArgumentToRefParameterMsg

[ref] isn’t case sensitive.

The only way round it that I know of is to create a blank Word document to use as a template

Copy-Item -Path mydoc.doc  -Destination testdoc.doc -Force            
            
$file = "C:\MyData\SkyDrive\Data\Scripts\Office-Word\testdoc.doc"            
            
$word = New-Object -ComObject "Word.application"            
$word.visible = $true            
$doc = $word.Documents.Open($file)            
            
$word.Selection.Font.Name = "Cambria"            
$word.Selection.Font.Size = "20"            
$word.Selection.TypeText("PowerShell")            
$word.Selection.TypeParagraph()            
            
$word.Selection.Font.Name = "Calibri"            
$word.Selection.Font.Size = "12"            
$word.Selection.TypeText("The best scripting language in the world!")            
$word.Selection.TypeParagraph()            
            
$doc.Save()            
$doc.Close()            
$Word.Quit()

Notice that you need to give the full path to the file. Use the Open method and add the text. You can then save, close and quit the application.

I’ve tested this using office 2010 & office 2013 on Windows 7 & 8

Unfortunately we are still left with the problem that we can’t save the Word document into different formats.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: