【问题标题】:I have a Run Time Error 91 for an Excel Add In我有一个 Excel 加载项的运行时错误 91
【发布时间】: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


【解决方案1】:

将所选内容导出为 PDF(从加载项运行)

  • 当没有打开未隐藏的工作簿并且 ActiveSheet是一张图表。
  • 文件夹不存在时出现不同的错误(运行时错误“1004”:文档未保存。文档可能已打开,或者保存时可能遇到错误。)或选择不是范围(运行时错误“438”:对象不支持此属性或方法)。
Option Explicit

Sub ExportSelectionToPDF()
    Const ProcName As String = "ExportSelectionToPDF"
    On Error GoTo ClearError

    Const dFolderPath As String = "H:\data\Desktop\"
    Const dFileExtension As String = ".pdf"
        
    If Len(Dir(dFolderPath, vbDirectory)) = 0 Then
        MsgBox "The path '" & dFolderPath & "' doesn't exist.", _
            vbCritical, ProcName
        Exit Sub
    End If
    
    Dim sh As Object: Set sh = ActiveSheet
    
    If sh Is Nothing Then ' to test, close all workbooks
        MsgBox "No active sheet ('Nothing').", vbCritical, ProcName
        Exit Sub
    End If
    
    If sh.Type <> xlWorksheet Then ' to test, activate a chart sheet
        MsgBox "No worksheet ('" & sh.Name & "') active.", vbCritical, ProcName
        Exit Sub
    End If
    
    If TypeName(Selection) <> "Range" Then ' to test, select a shape
        MsgBox "No range ('" & TypeName(Selection) & "') selected.", _
            vbCritical, ProcName
        Exit Sub
    End If
    
    Dim paAddress As String: paAddress = Selection.Address
    
    Dim BaseName As String: BaseName = sh.Parent.Name
    If InStr(BaseName, ".") > 0 Then
        BaseName = Left(BaseName, InStrRev(BaseName, ".") - 1)
    End If
    
    Dim dFilePath As String: dFilePath = dFolderPath & BaseName & dFileExtension
    
    With sh.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 = paAddress
    End With

    sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=dFilePath, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True
    
ProcExit:
    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Sub

【讨论】:

  • 感谢您的回复,当我运行此程序时,我得到 msgbox 没有活动工作表,但我确实打开了一个活动工作表并进行了选择
  • 代码有所不同。也许您在一个 Excel 实例中打开了加载项,而在另一个实例中打开了工作表。否则,您可以重新创建加载项或工作簿:某些内容似乎已损坏。我测试了我一直打开的加载项中的代码,并在活动(打开)工作簿上运行了代码。您使用的是哪个 Office 版本?这是一个有趣的link,关于与版本相关的实例。
【解决方案2】:

更新 03.01.2022: 注册加载项后,您是否再次打开了普通的 Excel 文件或加载项?您仍然需要一个普通的工作簿才能使用您的代码,因为加载项没有任何工作表。

一种可能的解决方案是以正确的方式加载加载项。如果您直接打开加载项,则会出现您的错误。您应该按照以下说明加载加载项:Microsoft Excel: Add or remove add-ins

【讨论】:

  • 感谢您的回复,我确实按照链接添加了它,但我仍然遇到同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多