【问题标题】:VBA code error file not found for shell command找不到 shell 命令的 VBA 代码错误文件
【发布时间】:2021-02-03 15:33:25
【问题描述】:

我已经尝试了很多方法来修复此错误。谁能看到为什么这段代码说找不到文件?

Sub PdfPwd()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim fTemp As String                                   'Defining Variables
    Dim oPdf As String
    Dim Pwd As String
    
    fTemp = "C:\Users\JSmith.ODINSU\Documents\" & "Temp.Pdf"
    oPdf = "C:\w2s\" & Sheets("w2 form").Range("q4").Value & ".pdf"                       'Set Path and Name for Protected Output PDF here.
    Pwd = Sheets("w2 form").Range("r4").Value                                         'Set appropriate Password here.
    
    With ActiveSheet                                      'Making a Temporary Unprotected Pdf file.
       .ExportAsFixedFormat Type:=xlTypePDF, _
                            Filename:=fTemp, _
                            Quality:=xlQualityStandard
    End With
    
'    fTemp = """" & fTemp & """"                           'Putting extra "" around for command Parameter.
'Debug.Print fTemp
'    oPdf = """" & oPdf & """"
'
'    Pwd = """" & Pwd & """"
                                                          'Making Command String for making protected PDFs Using PDFtk tool.
    cmdstr = "pdftk " & fTemp _
                      & " Output " & oPdf _
                      & " User_pw " & Pwd _
                      & " Allow AllFeatures"
Debug.Print cmdstr

    Shell cmdstr, vbHide                              'Executing PDFtk Command.
    
    Application.Wait DateAdd("s", 2, Now)                 'Allowing 2 secs for command to execute.
    
    Kill Replace(fTemp, """", "")                         'Deleting temporary files.
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    MsgBox "Finished", vbInformation
    
End Sub

这是我得到的文件路径。

pdftk C:\Users\JSmith.ODINSU\Documents\Temp.Pdf 输出 C:\w2s\10CARRACorrectedW2.pdf User_pw 123456 允许所有功能

当我将该行复制并粘贴到 cmd 并运行它时,一切正常。

【问题讨论】:

  • 我假设它找不到文件 pdftk.exe - 尝试包含 EXE 文件的完整路径。

标签: vba runtime-error pdftk


【解决方案1】:

我终于想通了。这是有效的代码。我排除了 end sub,因为我在代码中添加了一个电子邮件组件。

    Dim rng As Range
    
    Set rng = Sheets("w2 form").Range("at6:at9")
    
    For Each cell In rng
    
    fTemp = "C:\" & "Temp.Pdf"
    oPdf = "C:\w2s\" & Sheets("w2 form").Range("q4").Value & ".pdf"                       'Set Path and Name for Protected Output PDF here.
    Pwd = Sheets("w2 form").Range("r4").Value                                         'Set appropriate Password here.
    
    With ActiveSheet                                      'Making a Temporary Unprotected Pdf file.
       .ExportAsFixedFormat Type:=xlTypePDF, _
                            Filename:=fTemp, _
                            Quality:=xlQualityStandard
    End With
    
    fTemp = """" & fTemp & """"                           'Putting extra "" around for command Parameter.
Debug.Print fTemp
    oPdf = """" & oPdf & """"

    Pwd = """" & Pwd & """"
                                                          'Making Command String for making protected PDFs Using PDFtk tool.
    cmdstr = "pdftk " & fTemp _
                      & " Output " & oPdf _
                      & " User_pw " & Pwd _
                      & " Allow AllFeatures"
Debug.Print cmdstr

    Shell cmdstr, vbHide                              'Executing PDFtk Command.
    
    Application.Wait DateAdd("s", 2, Now)                 'Allowing 2 secs for command to execute.
    
    Kill Replace(fTemp, """", "")                         'Deleting temporary files.

【讨论】:

    【解决方案2】:

    我也遇到过同样的问题,其实是程序PDFtk的路径导致了错误。当您从 CMD 执行此操作时,它会捕获默认路径,但是当您使用 vba 执行此操作时,它会遵循 MS Office 路径,这就是它找不到应用程序的原因。

    使用此代码:

    cmdstr = "C:\Program Files\PDFtk\bin\pdftk.exe pdftk " ......
    

    而不是

    cmdstr = "pdftk " .....
    

    复制PDFtk.exe文件的完整路径(可以在program files文件夹中找到)并将其粘贴到上面提到的代码行中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2019-08-19
      • 2014-01-10
      • 2015-04-13
      相关资源
      最近更新 更多