Powershell - SaveAs function when file already exists
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 pdf's 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()
 


Powershell - SaveAs function when file already exists
===

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 pdf's 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()
 

