Option Explicit
Private WithEvents cmbrs As CommandBars
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private Sub Workbook_Activate()
Set cmbrs = Application.CommandBars
Call AddToPlyMenu
Call MonitorSheetTabsRightClick
End Sub
Private Sub Workbook_Deactivate()
Call DeleteFromPlyMenu
Set cmbrs = Nothing
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
Set cmbrs = Application.CommandBars
Call AddToPlyMenu
Call MonitorSheetTabsRightClick
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call DeleteFromPlyMenu
Set cmbrs = Nothing
End Sub
Private Sub cmbrs_OnUpdate()
Call MonitorSheetTabsRightClick
End Sub
Private Sub MonitorSheetTabsRightClick()
Dim sBuf As String * 256
Dim lRet As Long
With Application.CommandBars.FindControl(ID:=2020): .Enabled = Not .Enabled: End With
If ActiveWorkbook Is ThisWorkbook Then
lRet = GetClassName(GetActiveWindow, sBuf, 256)
With Application.CommandBars("Ply").Controls("Un&hide All")
If Left(sBuf, lRet) = "Net UI Tool Window" Then
.Visible = True
If AreSheetsProtected Then
.Enabled = True
Else
.Enabled = False
End If
End If
End With
End If
End Sub
Private Sub AddToPlyMenu()
Call DeleteFromPlyMenu
With Application.CommandBars("Ply").Controls.Add(Type:=msoControlButton, Temporary:=True, Before:=5)
.CAPTION = "Un&hide All"
.BeginGroup = True
.OnAction = Me.CodeName & ".Unhide_All_Sheets"
.Enabled = IIf(AreSheetsProtected, True, False)
End With
End Sub
Private Sub DeleteFromPlyMenu()
On Error Resume Next
Application.CommandBars("Ply").Reset
End Sub
Private Function AreSheetsProtected() As Boolean
Dim sh As Worksheet
For Each sh In Me.Worksheets
If sh.ProtectContents Then
AreSheetsProtected = True
Exit For
End If
Next
End Function
Private Sub Unhide_All_Sheets()
Dim sh As Worksheet
For Each sh In Me.Worksheets
If sh.ProtectContents Then
sh.Unprotect 'password
End If
Next
End Sub