Okay, so that didn't work at all like I was expecting.
I did however find a workaround to achieve what I was after, it's a bit messy and requires multiple saves, but was the only way I could think to achieve what I wanted.
So here's the bit of code I'm using that:
- hides the macro command button
- saves the file without a password
- saves a copy to an alternative location
- unhides the macro command button
- saves the file with the password to open
This gives me a copy of the file in an alternative location that can be opened without a password but the macros cannot be triggeres (everything is protected and can only be altered via macro):
...
'set filename and path variables
CurrFileName = CStr(ThisWorkbook.Name)
CurrFilePath = CStr(ThisWorkbook.Path)
RecsFilePath = CStr(ThisWorkbook.Worksheets("Workings").Range("nrPTRecsPTFold").Value) 'filepath of alternative location
ShortCurrFP = Right(CurrFilePath, Len(CurrFilePath) - 3) 'omits drive letter
'if the file name does not contain Master (first time the PT template is opened and saved by the tracker) or
'if the file path is not the same as the Rec's Folder (if the PT is opened and saved from this location)
'then save a copy to recs folder
If InStr(1, CurrFileName, "MASTER", vbTextCompare) = 0 And InStr(1, RecsFilePath, ShortCurrFP, vbTextCompare) = 0 Then
ThisWorkbook.Sheets("PT").Shapes("StampExit1").Visible = False
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, Password:=""
On Error GoTo RecsBackupErr
'non password protected backup copy
ThisWorkbook.SaveCopyAs Filename:=RecsFilePath & CurrFileName
On Error GoTo 0
ThisWorkbook.Sheets("PT").Shapes("StampExit1").Visible = True
ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, Password:=ThisWorkbook.Worksheets("Workings").Range("nrProtectionPW")
Application.DisplayAlerts = True
Cancel = True 'prevents saving again, after end / exit sub.
End If
End If
...
N.B. Application.EnableEvents set to false at start and true at the end
Hope this makes sense to anyone passing by.
Thanks, TC
Bookmarks