PowerShell 3 and Word

 

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.

Published Mon, Oct 15 2012 18:25 by RichardSiddaway

Comments

# re: PowerShell 3 and Word

Try it like this:

$file = [ref]"C:\Users\mark1976\Desktop\test1.doc"

$doc.SaveAs($file)

or like this:

[ref]$file = "C:\Users\mark1976\Desktop\test1.doc"

$doc.SaveAs($file)

or like this:

$file = "C:\Users\mark1976\Desktop\test1.doc"

$doc.SaveAs(([ref]$file))

tested and works with Office 2010 and Powershell V3 on Windows 7

(and thank you for a great blog :-)

Wednesday, November 21, 2012 8:13 AM by Mark Mortensen

# re: PowerShell 3 and Word

Hi Mark

Thanks for the comment and the workaround.  I've tested it on Windows 8, PS v3 and Office 2013 and works.

Thursday, November 22, 2012 1:36 PM by RichardSiddaway

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: