Mike L
Mike L

Reputation: 496

With Block Variable not Set -- Error when workbook Opened

This macro is one that was not written by me, so I'm having trouble understanding the source of the error. I have a macro that's supposed to run on startup to adjust the ribbon to add a button, and another part to remove styles when you select that button. Currently, I get the message: Object variable or With block variable not set. When I select "Debug" it goes to the VBA screen and immediately gives me 3 more error pop-ups that say: Can't execute code in break mode.

The first part of this is the two subs that are to run on startup, which are:

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

It highlights the Module1.MyRibbon.Invalidateas the problematic bit. Personally I don't see anything wrong with this per se, but perhaps the problem is in the Module 1? That code contains three subs, as follows:

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

I've never written any Activation or Ribbon-related macro, so I have no idea where the error could be. The addin works just find regardless of this message, as the button gets added and it functions as it should when the file isn't a blank file, but I get the error pop-up and the button doesn't get created right on new, blank files. How could I fix this?

Upvotes: 0

Views: 1095

Answers (1)

Andrew Caprario
Andrew Caprario

Reputation: 92

I simply deleted:

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

No runtime errors on start of excel and no issues when using the script; counts fine and deletes fine. Windows 7, Excel 2010.

Upvotes: 1

Related Questions