I have a large workbook, wherein I use UserInterfaceOnly on the sheets implemented through the Workbook_Activate event. Up until now it's always worked just fine, but now it doesn't work in a specific circumstance.
When I open the file directly or with a shortcut or with the File->Open command, it works just fine.
When I open it with a custom ribbon button (which has always worked fine), the workbook opens and both the Open and Activate events run, but the protection isn't applied. So most of the sheets will have full protection, and if the Workbook was closed with protection removed on a a sheet (usually during troubleshooting) then it opens without protection.
The routines are pretty simple, but I'll copy them here so I don't get requests for them.
First, the ThisWorkbook module:
Then the protection routine:
Then the CustomUI for the ribbon group (The workbook in question is the Operations one):
Here is the relevant callback and code it calls:
Now, here's what I was doing that precipitated this all happening.
1) I inserted a new sheet, with some professional custom controls (not created by me), that originally had some excel charts in them, but they were deleted. I have since gone back in and deleted the shapes that were ghosted in.
2) While doing this I notice that my original custom menu from when this was an Excel 2003 sheet was still in there, so I deleted that module and sheet. There is no code connection between those and the ribbon.
Any ideas on why opening this workbook through the ribbon button won't implement protection?</button></group></tab></tabs></ribbon></customui>
When I open the file directly or with a shortcut or with the File->Open command, it works just fine.
When I open it with a custom ribbon button (which has always worked fine), the workbook opens and both the Open and Activate events run, but the protection isn't applied. So most of the sheets will have full protection, and if the Workbook was closed with protection removed on a a sheet (usually during troubleshooting) then it opens without protection.
The routines are pretty simple, but I'll copy them here so I don't get requests for them.
First, the ThisWorkbook module:
Code:
Option Explicit
Private Sub Workbook_Activate()
Application.ScreenUpdating = False
Dim wsheet As Worksheet
For Each wsheet In ThisWorkbook.Worksheets
Call ProtectSheetInterface(wsheet)
Next wsheet
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Open()
Call WidenNameBoxDrop2
End Sub
Then the protection routine:
Code:
Public Sub ProtectSheetInterface(wsheet As Worksheet)
wsheet.Protect Password:="xxxxxx", UserInterfaceOnly:=True
End Sub
Then the CustomUI for the ribbon group (The workbook in question is the Operations one):
Code:
<customui xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<!-- Add Custom group to the Home tab in the ribbon -->
<!-- This is for switching between workbooks in Dispatch Accounting, and for saving them -->
<ribbon>
<tabs>
<tab idmso="TabHome">
<group id="GoToGroup" label="GoTo" insertaftermso="GroupEditingExcel">
<button id="OperationsButton" label="Operations" onaction="GoToOperations">
</button><button id="ForecastButton" label="Forecast" onaction="GoToForecast">
</button><button id="LogsButton" label="Logs" onaction="GoToLogs">
<separator id="MySeparator1">
</separator></button><button id="CgasButton" label="Cgas" onaction="GoToCgas">
</button><button id="SaveAllButton" label="Save All" onaction="SaveAllWorkbooks">
</button><button id="SaveCloseButton" label="Save Close" onaction="SaveClose">
Here is the relevant callback and code it calls:
Code:
Public Sub SwapWorkbooks(fname As String, newpath As String)
'Windows(1).WindowState = xlMinimized
On Error GoTo OpenTheSheet
Workbooks(fname).Activate
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Windows(1).Visible = True
Windows(1).WindowState = xlMaximized
Exit Sub
OpenTheSheet:
Workbooks.Open Filename:=newpath, UpdateLinks:=xlUpdateLinksAlways
Workbooks(fname).Activate
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Windows(1).Visible = True
Windows(1).WindowState = xlMaximized
ActiveWindow.DisplayWorkbookTabs = True
ActiveSheet.EnableSelection = xlNoRestrictions
End Sub
Public Sub GoToOperations(control As IRibbonControl)
Call SwapWorkbooks("Operations.xlsm", ThisWorkbook.Path + "\Operations.xlsm")
End Sub
Now, here's what I was doing that precipitated this all happening.
1) I inserted a new sheet, with some professional custom controls (not created by me), that originally had some excel charts in them, but they were deleted. I have since gone back in and deleted the shapes that were ghosted in.
2) While doing this I notice that my original custom menu from when this was an Excel 2003 sheet was still in there, so I deleted that module and sheet. There is no code connection between those and the ribbon.
Any ideas on why opening this workbook through the ribbon button won't implement protection?</button></group></tab></tabs></ribbon></customui>