【发布时间】:2022-01-27 20:32:51
【问题描述】:
我花了几个小时试图调试它。这是一个从我选择的单元格生成 PDF 的宏。此代码适用于我的个人工作簿,但是当我将其导出为加载项时,将其添加到开发人员选项卡中并在加载项工作簿中再次查看代码我不断收到运行时错误 91:对象变量或块变量没有设置。任何帮助将不胜感激!
Sub Save_Selection_As_PDF_sheet()
Dim my_file As String
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
'.Orientation = xlLandscape
.Orientation = xlPortrait
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintArea = Selection.Address
Debug.Print I
End With
FileName = ActiveWorkbook.Name
If InStr(FileName, ".") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
my_file = "H:\data\Desktop\" & FileName & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=my_file, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
【问题讨论】:
-
哪一行报错?
-
@BigBen 在“With ActiveSheet.PageSetup”之后
标签: excel vba runtime-error add-in