未设置块变量 - 打开工作簿时出错

With Block Variable not Set -- Error when workbook Opened

这个宏不是我写的,所以我无法理解错误的来源。我有一个宏,应该在启动时 运行 调整功能区以添加按钮,而另一部分在您 select 该按钮时删除样式。目前,我收到消息:Object variable or With block variable not set。当我 select "Debug" 它进入 VBA 屏幕并立即给我 3 个错误弹出窗口说: Can't execute code in break mode.

这部分的第一部分是启动时要 运行 的两个子程序,它们是:

Dim WithEvents app As Application

Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
  Module1.MyRibbon.Invalidate
End Sub

Private Sub Workbook_Open()
  Set app = Application
End Sub

它突出显示 Module1.MyRibbon.Invalidate 作为有问题的位。我个人认为这本身没有任何问题,但问题可能出在模块 1 中?该代码包含三个子,如下所示:

Public MyRibbon As IRibbonUI
'Callback for customUI.onLoad
Sub CallbackOnLoad(Ribbon As IRibbonUI)
    Set MyRibbon = Ribbon
End Sub


'Callback for customButton getLabel
Sub GetButtonLabel(control As IRibbonControl, ByRef returnedVal)
   If ActiveWorkbook Is Nothing Then
        returnedVal = "Remove Styles"
   Else
        returnedVal = "Remove Styles" & vbCr &  
           Format(ActiveWorkbook.Styles.Count, "#" & Application.International(xlThousandsSeparator) & "##0")
   End If
End Sub


Sub RemoveTheStyles(control As IRibbonControl)
    Dim s As Style, i As Long, c As Long
    On Error Resume Next
    If ActiveWorkbook.MultiUserEditing Then
        If MsgBox("You cannot remove Styles in a Shared workbook." & vbCr & vbCr & _
              "Do you want to unshare the workbook?", vbYesNo + vbInformation) = vbYes Then
        ActiveWorkbook.ExclusiveAccess
           If Err.Description = "Application-defined or object-defined error" Then
               Exit Sub
           End If
        Else
           Exit Sub
        End If
    End If
    c = ActiveWorkbook.Styles.Count
    Application.ScreenUpdating = False
    For i = c To 1 Step -1
       If i Mod 600 = 0 Then DoEvents
       Set s = ActiveWorkbook.Styles(i)
       Application.StatusBar = "Deleting " & c - i + 1 & " of " & c & " " & s.Name
       If Not s.BuiltIn Then
           s.Delete
           If Err.Description = "You cannot use this command on a protected sheet. To use this command, you must first unprotect the sheet (Review tab, Changes group, Unprotect Sheet button). You may be prompted for a password." Then
            MsgBox Err.Description & vbCr & "You have to unprotect all of the sheets in the workbook to remove styles.", vbExclamation, "Remove Styles AddIn"
               Exit For
           End If
        End If
    Next
    Application.ScreenUpdating = True
    Application.StatusBar = False
End Sub

我从来没有写过任何激活或功能区相关的宏,所以我不知道错误可能在哪里。无论此消息如何,插件都可以正常工作,因为添加了按钮并且当文件不是空白文件时它可以正常工作,但是我收到错误弹出窗口并且没有在新文件上正确创建按钮, 空白文件。我该如何解决这个问题?

我干脆删了:

    Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
  Module1.MyRibbon.Invalidate
End Sub

excel 启动时没有运行时错误,使用脚本时也没有问题;算得好,删除得好。 Windows 7, Excel 2010.