3

I'm trying to run some code that looks for all .doc & .docx files in a directory & sub-directories and then converts each one to PDF format.

The code below works only if there are no instances of the pdf in these directories i.e. it only works first time. Every subsequent time it fails with:

Exception calling "SaveAs" with "2" argument(s): "Command failed" At C:\convert\convertword.ps1:12 char:13 + $doc.saveas <<<< ($path, $wdFormatPDF) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

When I delete the previously created PDFs and re-run the PS it works fine. Therefore I can only assume there is a switch or parameter that I'm missing from my SaveAs function which somehow forces the overwrite?

$wdFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "c:\convert\*"
$fileTypes = "*.docx","*doc"
Get-ChildItem -path $folderpath -recurse -include $fileTypes |
foreach-object `
{
$path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
"Converting $path to pdf ..."
$doc = $word.documents.open($_.fullname)
$doc.saveas($path, $wdFormatPDF) 
$doc.close()
}
$word.Quit()
Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
SuperSub
  • 87
  • 1
  • 2
  • 7

3 Answers3

2

Ok I finally think I've tracked down the problem. It's the Windows Explorer Preview Pane which is locking the file. I had show preview pane turned on the directory where the files were being created and converted, this must have been creating a file lock on the pdf's therefore the script cannot save the new pdf. I turned off preview pane in my Windows Explorer and the script now works repeatedly! Therefore nothing wrong with the Powershell Scripting but thanks for all the input guys. Here's a link to the closest MS KB article that I could find on the subject http://support.microsoft.com/kb/942146

SuperSub
  • 87
  • 1
  • 2
  • 7
1

try this:

$word.displayalerts = $false
$doc.saveas($path, $wdFormatPDF) # with Word2010 I've to use  $doc.saveas([ref]$path, [ref]$wdFormatPDF)
$word.displayalerts = $true

No error is raised, but I'm using Word2010 I can't test it with other versions

CB.
  • 58,865
  • 9
  • 159
  • 159
  • Sorry for delayed response I've been away. Tried application.displayalerts but got the following The term 'application.displayalerts' is not recognized as the name of a cmdlet, function , script file, or operable program. Check the spelling of the name, or if a path was inc luded, verify that the path is correct and try again. – SuperSub Apr 30 '12 at 13:55
  • Sorry, my bad paste&copy! Try now! – CB. Apr 30 '12 at 14:01
  • Sorry no It's changed the error back to the original Exception calling "SaveAs" with "2" argument(s): "Command failed" At C:\convert\convertword.ps1:13 char:13 + $doc.saveas <<<< ($path, $wdFormatPDF) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation – SuperSub Apr 30 '12 at 14:28
  • @SuperSub try using [ref] in parameter, like in my commented code, om my box windows 7 and office2010 works! – CB. Apr 30 '12 at 14:33
  • Argument: '1' should not be a System.Management.Automation.PSReference. Do not use [ref] . – SuperSub Apr 30 '12 at 15:51
  • Interestingly if I manually try to Save As from my word Document to PDF format after I select 'PUBLISH' I get a MS Office Word warning "The file is in use by another application or user". However if I delete the exisiting PDF file then Save as PDF it work fine. – SuperSub Apr 30 '12 at 15:54
  • @supersub I can't help you more! For me works with no error passing two [ref] as param? Have you tried just second param as [ref]? – CB. Apr 30 '12 at 16:47
0

There's no flag to overwrite according to the documentation for SaveAs and SaveAs2. So you could just remove it before saving with something like this:

Remove-Item -Path $path -Force -ErrorAction SilentlyContinue
$doc.saveas ($path, $wdFormatPDF) 
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Added the RemoveItem line of code and now have a different errorUnexpected token '(' in expression or statement. At C:\convert\convertword.ps1:13 char:15 + $doc.saveas ( <<<< $path, $wdFormatPDF) + CategoryInfo : ParserError: ((:String) [], ParentContainsErrorRecordExcept ion + FullyQualifiedErrorId : UnexpectedToken – SuperSub Apr 25 '12 at 15:36
  • I don't know if your actual code is like that or not, but what you posted has a space between the end of SaveAs and the (). – EBGreen Apr 25 '12 at 15:43
  • The docs for Word 2010 say that SaveAs by default should overwrite an existing file - `"If a document with the specified file name already exists, the document is overwritten without the user being prompted first."` Go figure. – Keith Hill Apr 25 '12 at 15:49
  • Ah yes well spotted EBGreen! Nevertheless I've removed the extra space and now back to the same error as original. – SuperSub Apr 25 '12 at 15:51
  • Yes Keith Hill that is what my understanding of SaveAs - Unless the the fact that I only get an error when the file already exists is a Red Herring on this occasion? – SuperSub Apr 25 '12 at 15:55