tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
I have a workbook containing two mandatory worksheets.
I also have a custom ribbon with a single Delete button.
I want the button to be activated if the activesheet is NOT one of the two manadatory sheets.
This is in ThisWorkbook:
and this is Module1:
This is the xml:
Everything works as expected but I don't understand this sub:
The program crashes if it were:
This is caused (I think) by:
which IS necessary because this Workbook_Open event is NOT present, if the workbook is saved with the activesheet NOT being one the two mandatory sheets, (then closed and reopend, the delete button will be disabled.
But why would MyRibbon = Nothing when the workbook is first opened?
Thanks
I also have a custom ribbon with a single Delete button.
I want the button to be activated if the activesheet is NOT one of the two manadatory sheets.
This is in ThisWorkbook:
Code:
Option Explicit
Private Sub Workbook_Open()
Call Temp
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call Temp
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Call Module1.UpdateCombo
End Sub
Private Sub Temp()
Dim Flag As String
Select Case ActiveSheet.CodeName
Case wksMandatory1.CodeName, wksMandatory2.CodeName
Flag = "Disable"
Case Else
Flag = "Enable"
End Select
Call Module1.UpdateDelete(Tag:=Flag)
End Sub
and this is Module1:
Code:
Option Explicit
Dim MyRibbon As IRibbonUI
Dim Status As String
Public Sub Initialise(ByRef ribbon As IRibbonUI)
Set MyRibbon = ribbon
End Sub
Public Sub UpdateCombo()
MyRibbon.InvalidateControl "Combo"
End Sub
Public Sub Delete_Sheet(ByRef control As IRibbonControl)
ActiveSheet.Delete
End Sub
Public Sub UpdateDelete(Tag As String)
Status = Tag
If Not MyRibbon Is Nothing Then Call MyRibbon.Invalidate
End Sub
Private Sub GetEnabledMacro(ByRef control As IRibbonControl, _
ByRef returnedVal As Variant)
If control.Tag = "Delete" And _
Status = "Enable" Then
returnedVal = True
Else
returnedVal = False
End If
End Sub
This is the xml:
Code:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"
onLoad="Initialise">
<ribbon startFromScratch="false">
<tabs>
<tab id="Test"
label="Test">
<group id="Group7"
label="Sheets">
<button id="customButton7"
label="Delete Sheet"
image="DeleteSheet"
size="large"
onAction="Delete_Sheet"
getEnabled="GetEnabledMacro"
tag="Delete"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Everything works as expected but I don't understand this sub:
Code:
Public Sub UpdateDelete(Tag As String)
Status = Tag
If Not MyRibbon Is Nothing Then Call MyRibbon.Invalidate
End Sub
The program crashes if it were:
Code:
Public Sub UpdateDelete(Tag As String)
Status = Tag
Call MyRibbon.Invalidate
End Sub
This is caused (I think) by:
Code:
Private Sub Workbook_Open()
Call Temp
End Sub
which IS necessary because this Workbook_Open event is NOT present, if the workbook is saved with the activesheet NOT being one the two mandatory sheets, (then closed and reopend, the delete button will be disabled.
But why would MyRibbon = Nothing when the workbook is first opened?
Thanks