Saving Workbooks On Workbook Open Event

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have two workbooks that I fear other users may unintentionally change and then save the changes that shouldn't have been made.
Is there a way that when the users opens the workbook, before they have a chance to monkey with it, that original workbook is saved elsewhere with a different name?
I thought this ...
Code:
dt = Format(Now(), "dd-mmm-yy hh:mm:ss")
myFileName = ActiveWorkbook.Name & dt
ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlWorkbookNormal, Password:="31286", addToMru:=False

This might work, but it will change the name of the activeworkbook, which will cause all kinds of problems in code needing the original file's name.

The second workbook isn't open yet. Is there a way I can copy it from it's directory, pasted into a second directory and renamed?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The main problem I see with that is that you're trying to append a date after the extension, so the result would be
MyFile.xlsx08-Dec-22 08:22:33 - not possible. First maybe think about whether or not you really need the time value. If so, I suppose you want to add another file and not over-write an existing one.
 
Upvote 0
Also, you cannot use : in a file name, and when you do a save as, it becomes the current workbook. Is that what you want?
Beginning to think you want to use File Scripting Object to save a copy and keep the opened wb as the current session, or use SaveCopyAs, not SaveAs.
 
Last edited:
Upvote 0
Here is a code sample using SaveCopyAs and an altered format that doesn't use :
VBA Code:
Private Sub Workbook_Open()
Dim strName As String, strExtension As String

strName = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".")) - 1)
strExtension = Mid(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".")))
strName = "C:\Temp\" & strName & " " & Format(Now, "dd-mm-yy hh_mm_ss") & strExtension
ActiveWorkbook.SaveCopyAs strName
End Sub
Create workbook open event, replace C:\Temp\ with the path location that you need. It should work for any Excel extension. Alter format of date/time details as required. Test with a copy of a file.
Hope that's what you're after.
 
Upvote 0
Another approach you may consider is to make the workbook ReadOnly when opened by other users.
VBA Code:
Private Sub Workbook_Open()
    If Environ("username") <> "Enter_Your_Windows_Login_UserName_Here" Then
        Application.DisplayAlerts = False
        Me.ChangeFileAccess xlReadOnly
        Application.DisplayAlerts = False
    End If
End Sub
Now, if the users try to save any changes they made to the workbook, they will be prompted to save under a different name hence leaving your original workbook intact.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top