2015-11-12 31 views
0

該宏不是由我編寫的,所以我無法理解錯誤的來源。我有一個應該在啓動時運行的宏,用於調整功能區以添加按鈕,另一部分用於在選擇該按鈕時刪除樣式。目前,我收到消息:Object variable or With block variable not set。當我選擇「調試」時,它進入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 

我沒寫過任何激活或絲帶相關的宏,所以我不知道在哪裏的錯誤可能。插件的工作只是找到,不管這個消息,因爲按鈕被添加,它的功能應該是當文件不是空白文件,但我得到錯誤彈出,並且按鈕不會創建正確的新,空白文件。我怎麼能解決這個問題?

+0

您需要找到CallbackOnLoad的執行位置。 否則MyRibbon不會啓動。 –

+0

沒有讀完你的完整腳本,我可以猜測。你用'Module1.'來限定錯誤,我不相信你需要這樣做。你可以試試'MyRibbon.Invalidate'嗎? – nbayly

+0

我試過MyRibbon.Invalidate,它產生了同樣的錯誤。 –

回答

1

只是刪除:

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

沒有運行時錯誤上的Excel的啓動和使用腳本的時候沒有問題;計數良好並刪除罰款。 Windows 7,Excel 2010.