Hi,
I have a Tracker workbook that opens another workbook (PT, as master copy), copies across some details, creates an appropriate directory and saves the PT workbook under another name (client and case number).
The PT workbook requires a password to open, has its own macro and has SaveAsUI 'disabled'. The population and general control of the PT workbook is handled by the Tracker workbook - but the user has the ability to 'stamp' the PT workbook to update the status of a process.
So far this all works fine. The trouble comes when I try and save a copy of the PT workbook to another location (different drive share, for a different audience). I want this PT Copy to be updated (overwritten) each time the workbook is saved to this secondary location. Sometimes prompted by the Tracker macro sometimes by the user directly - it doesn't make any difference.
I have used SaveCopyAs in the past but it won't work in this case as I need to remove the password to open file and prevent the user from running any macros. I've tried using SaveAs and CreateBackup but it doesn't behave how I would expect / need, and haven't been able to find out much on the interweb-googly-whatsit.
This is the code I currently have in the PT workbook:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'prevents user using SAVE AS - the Tracker must control underlying file and folder structure to function effectively - WORKS FINE
If SaveAsUI = True Then
MSG1 = MsgBox("You CANNOT save another version of this PT - you can only save changes to the existing version." & vbNewLine _
& vbNewLine & "Please use 'SAVE' and NOT 'SAVE AS'", vbCritical, "Save As NOT allowed")
Cancel = True
GoTo Abort
End If
'if the file name contains Master (first time the PT template is opened and saved by the Tracker) skip to abort (dont save a copy to recs destination) - WORKS FINE
CurrFileName = CStr(ThisWorkbook.Name)
If InStr(1, CurrFileName, "MASTER", vbTextCompare) <> 0 Then GoTo Abort
ThisWorkbook.Sheets("PT").Shapes("StampExit1").Visible = False 'hide the command button on the spreadsheet - prevents user triggering code
Application.DisplayAlerts = False
On Error GoTo RecsBackupErr
'non password protected backup copy, file path stored in named range - DOESN'T WORK HOW I WOULD EXPECT
ThisWorkbook.SaveAs Filename:=ThisWorkbook.Worksheets("Workings").Range("nrPTRecsPTFold") & ThisWorkbook.Name, CreateBackup:=True
On Error GoTo 0
Application.DisplayAlerts = True
Abort:
ThisWorkbook.Sheets("PT").Shapes("StampExit1").Visible = True
Exit Sub
RecsBackupErr: 'WORKS FINE
MSG1 = MsgBox("An error occurred in saving a copy of the PT into the Recs folder, the file may not have been saved. " _
& vbNewLine & vbNewLine & "Please check and try again - saving the file will automatically back up to the Recs folder", vbCritical, "File Save Error")
ThisWorkbook.Sheets("PT").Shapes("StampExit1").Visible = True
Application.DisplayAlerts = True
End Sub
When this code is executed the Recs copy is saved but retains password protection and the Command Button (StampExit1) is still visible - exactly the same as the normal working version.
Am I barking up the wrong tree here? I tried going down the route of copying the sheet contents to another workbook (but sometimes there are more sheets than one) and it's a pain to change the colour pallette, formatting, delete hidden rows & columns etc, etc. It just feels so clunky and wrong.
I'm after SaveCopyAs functionality but with the password removed, the command button hidden and the 'main working copy' of the file to remain open for the user.
Is this possible? Feel like I'm missing something here...
Thanks as always, TC
Bookmarks