【问题标题】:Exporting excel sheet as PDF Run-time error '5': Invalid procedure call or argument将 Excel 工作表导出为 PDF 运行时错误“5”:过程调用或参数无效
【发布时间】:2016-05-27 16:01:14
【问题描述】:

我在将工作簿中的一张 Excel 工作表导出为 PDF 文件时遇到问题。

Sub ExportPDF()
    Dim Nazwa As String
    ' Creating a destination folder for PDF file
    If Len(Dir("c:\Faktury", vbDirectory)) = 0 Then
        MkDir "c:\Faktury"
    End If
    'InputBox that allows user to type filename
    Nazwa = InputBox("Wpisz nazwę pliku", "Nazwa pliku", Sheets("Fa VAT").Range("G3"))
    If Nazwa = "" Then Exit Sub
    ChDir "c:\Faktury"
    'Exporting sheet "Fa VAT" to PDF
    Sheets("Fa VAT").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=Nazwa, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub

我收到错误“运行时错误'5':无效的过程调用或参数”。之后,Excel 会突出显示代码的最后一部分,因为它应该是错误的。我的意思就是这部分:

    Sheets("Fa VAT").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=Nazwa, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

我安装了 Microsoft SaveAsPDF 设置,但没有任何变化。我将文件名更改为一些随机字符串,而不是引用 InputBox。我试图激活正确的工作表,然后导出 ActiveSheet。我尝试隐藏和显示工作表。我尝试在另外两台计算机上运行该文件。

当我将此宏复制到某个全新的空白 Excel 电子表格时,它可以工作。

我在 Excel 2007、Windows 10 上工作。我也在 E​​xcel 2010 和 Windows 7 上尝试过。

以下是我尝试过的一些链接:

Runtime Error : 5, Invalid Procedure call or argument

http://www.mrexcel.com/forum/excel-questions/595252-visual-basic-applications-trouble-save-pdf-code.html

【问题讨论】:

    标签: vba excel pdf


    【解决方案1】:

    确保工作表没有隐藏。如果工作表隐藏或非常隐藏,Excel 将为 .ExportasFixedFormat 引发错误。如果你的隐藏了,你可以先在你的代码中取消隐藏:

    Sheets("Fa VAT").Visible = xlSheetVisible

    最后,你可以再次隐藏它。

    Sheets("Fa VAT").Visible = xlSheetHidden

    【讨论】:

      【解决方案2】:

      我有同样的错误。就我而言,给出的文件路径和名称太长了。缩短名称或路径,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-08
        • 1970-01-01
        • 2019-06-13
        • 1970-01-01
        相关资源
        最近更新 更多