Forum Discussion

Jianzhong_Hill's avatar
Jianzhong_Hill
Copper Contributor
May 08, 2023

Sorry, we couldn't find your file. Was it moved, renamed, or deleted?

I want to write a script using powershell which can convert all word files in one folder into PDF format, But it always went error like blow
 

Sorry, we couldn't find your file. Was it moved, renamed, or deleted? (C:\Users\PC\Desktop\test\N1.docx
)
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:10 char:5
+ $document = $wordObject.Documents.Open($wordPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:11 char:5
+ $document.SaveAs([ref] $pdfPath, [ref] 17)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:12 char:5
+ $document.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Sorry, we couldn't find your file. Was it moved, renamed, or deleted? (C:\Users\PC\Desktop\test\N2.docx
)
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:10 char:5
+ $document = $wordObject.Documents.Open($wordPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:11 char:5
+ $document.SaveAs([ref] $pdfPath, [ref] 17)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\PC\Desktop\Powershell\Converter_0.ps1:12 char:5
+ $document.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

 

Here is my script
$folderPath = "C:\Users\PC\Desktop\test"
$folderPathOut = "C:\Users\PC\Desktop\Output"
$wordFiles = Get-ChildItem -Path $folderPath -Filter "*.docx"
$wordObject = new-object -ComObject "Word.Application"
$wordObject.Visible = $True
foreach ($file in $wordFiles)
{
    $wordPath = $file.DirectoryName+'\'+$file.Name | Out-String
    $pdfPath = Join-Path -Path $folderPathOut -ChildPath ($file.BaseName + ".pdf") | Out-String
    $document = $wordObject.Documents.Open($wordPath)
    $document.SaveAs([ref] $pdfPath, [ref] 17)
    $document.Close()
}
$wordObject.Quit()
 
:sad:

2 Replies

  • gongyan's avatar
    gongyan
    Copper Contributor

    Jianzhong_Hill 

    看你的昵称,你好像是中国人?如果是的话,我也是。

    你的问题在于这一行代码:

     

    $document = $wordObject.Documents.Open($wordPath)

     

    $wordPath变量是不能被open函数识别的,你应该这样写:

     

    $document = $wordObject.Documents.Open($wordPath.fullname)

     

     我在我自己的电脑上解决了你说的文件不存在的报错,但是随之而来的是新的错误:

     

    Exception Caught:  System.Runtime.InteropServices.COMException (0x80010001): 被呼叫方拒绝接收呼叫。 (异常来自 HRESULT:0x80010001 (RPC_E_CALL_REJECTED))
       在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       在 System.Management.Automation.ComInterop.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, ComMethodDesc method, Object[] args, UInt32 argErr)
       在 CallSite.Target(Closure , CallSite , ComObject , Object , Int32 )
       在 System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       在 System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       在 System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
       在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

     

     今天的时间不够了,明天我在尝试解决这个问题。

    今天终于解决了这个问题,途中我被我自己的一个误操作困扰了很久,但是最终解决掉了,值得吐槽的是,com真难用,明明是参数1的错误,非要说第二个参数错了。

    我的代码:

    $folderPath = "D:\工作\temp\2023年4月19日"
    $folderPathOut = "D:\工作\temp\2023年4月19日"
    $wordFiles = Get-ChildItem -Path $folderPath -Filter "*.docx"
    try{
        Get-Process -Name "*word*" |Stop-Process         #避免之前运行失败的word程序影响
        $wordObject = new-object -ComObject "Word.Application"
    }
    catch {
        Write-Host "Exception Caught: " $_.Exception -ForegroundColor Red
        exit
    }
    foreach ($file in $wordFiles){
        $pdfPath = Join-Path -Path $folderPathOut -ChildPath ($file.BaseName + ".pdf")
        try{
            $document = $wordObject.Documents.Open($file.FullName)
        }
        catch {
            Write-Host "打开word文件: " $_.Exception -ForegroundColor Red
            continue
        }
        try{
            $document.SaveAs($pdfPath, 17)
        }
        catch {
            Write-Host "另存为: " $_.Exception -ForegroundColor Red
        }
        finally{
            $document.Close()
        }
    }
    $wordObject.Quit()
    Remove-Variable wordObject

    我在我的电脑上测试通过:Windows 10 企业版,word 2019

     

  • Jianzhong_Hill's avatar
    Jianzhong_Hill
    Copper Contributor
    By the way, I check my folder and file for many times.
    They are there correctly i promise that!
    When i CTRL+click the "(C:\Users\PC\Desktop\test\N1.docx)" in my VScode terminal, it can open N1.docx file correctly...

Resources