将范围导出为pdf时出现运行时错误

时间:2014-11-18 11:49:51

标签: excel vba excel-vba

当我尝试将片材中的'sheet'/'范围'作为pdf导出时,无论我将代码保存在工作表或不同的模块中,无论我给出什么路径,都会出现运行时错误。我尝试了很多代码。在下面举几个。我总是得到一个错误。知道为什么吗?

错误
运行时错误'5';
无效的过程调用或参数

我尝试的代码:

Sub try()
Sheets("Sheet1").Range("A1:F10").ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
    "c:\Book1.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
End Sub

Sub luxation()
    ThisWorkbook.Sheets("Sheet1").Range("A1:F10").Select
    Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        fileName:="temp.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
End Sub

Sub Sample()
    ActiveWorkbook.Sheets("Sheet2").Activate

    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    fileName:=ActiveWorkbook.Path & "\Survey Report.pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub

1 个答案:

答案 0 :(得分:1)

以下步骤将导出到指定位置PDF:

'rng - The range you wish to be exported'
'strFP - The file path to save the PDF to'

Public Sub fExportPDF(rng As Range, strFP As String)

'Export as PDF
With rng
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFP, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
End With

End Sub

要使用该程序,请这样调用:

Sub SomeProc()

fExportPDF ThisWorkbook.Worksheets(1).Range("A1:B3"), "C:\Some Location\example.pdf"

End Sub