【问题标题】:Create PDF compile error "Application: defined or object-defined error"创建 PDF 编译错误“应用程序:定义或对象定义错误”
【发布时间】:2020-12-12 09:29:48
【问题描述】:

我有一个 VBA 用户表单,用户可以在其中搜索数据库表中的某些术语。它在单独的表格中生成结果,当用户单击打印按钮时,它会创建一个 PDF,其中包含搜索结果表格中的所有数据。

编译错误

“应用程序:定义的或对象定义的错误”

Sub CreatePDF()

    Sheet4.Select
    Range("A1:N25").Select
 

        ThisWorkbook.Sheets("SearchData").Select
        Selection.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
            Quality:=xlQualityStandard, _
            Orientation:=xlLandscape, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
               
       
    Sheet4.Select
    Range("A1").Select

End Sub

突出显示的行是:

Selection.ExportAsFixedFormat _ Type:=xlTypePDF, _             
  Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _             
  Quality:=xlQualityStandard, _
  Orientation:=xlLandscape, _
  IncludeDocProperties:=True, _
  IgnorePrintAreas:=False, _
  OpenAfterPublish:=True

【问题讨论】:

  • 突出显示哪一行?
  • Selection.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _ Quality:=xlQualityStandard, _ Orientation:=xlLandscape, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True

标签: excel vba pdf


【解决方案1】:

方向

PageSetup.Orientation

Range.Orientation

Worksheet.ExportAsFixedFormat method 中没有 Orientation 参数。
你可以试试这个:

Option Explicit

Sub CreatePDF()

    With ThisWorkbook.Worksheets("SearchData")
        .PageSetup.Orientation = xlLandscape
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
    End With
    
    With Sheet4
        .Activate
        .Range("A1").Select
    End With

End Sub

或者如果你想打印范围:

Sub CreatePDF2()
 
    With Sheet4.Range("A1:N25")
        .Worksheet.PageSetup.Orientation = xlLandscape
        .Orientation = xlHorizontal
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
        .Worksheet.Activate
        .Cells(1).Select   
    End With

End Sub

【讨论】:

  • 谢谢,第一个完美无缺。
  • @kamranmirza ,请标记为答案,以便线程显示为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-05-04
相关资源
最近更新 更多