Copying file to another location

  • Thread starter Thread starter selsley
  • Start date Start date
S

selsley

Can anyone tell me how to copy an Excel file to another location when
open it? I'm trying to allow people to view a copy of a file each tim
I update it.

Many thanks

Stev
 
Wouldn't you want to copy your workbook to that location when you save it--not
when you open it?

I put this code behind the ThisWorkbook module:

Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.OnTime Now + TimeSerial(0, 0, 3), "SaveACopy"
End Sub

Tnen in a general module, I put this code:

Option Explicit
Sub SaveACopy()

Dim myPath As String

myPath = "c:\my documents\excel\test\"

With ThisWorkbook
If .Saved Then
On Error Resume Next
.SaveCopyAs Filename:=myPath & .Name
If Err.Number <> 0 Then
MsgBox "Something bad happened"
Err.Clear
Else
MsgBox "Also saved to: " & myPath & .Name
End If
On Error GoTo 0
End If
End With

End Sub

If you save your workbook, then it does the SaveCopyAs to the location of your
choice (about 3 seconds after the save).
 
The bad news about this is that the code will travel with the workbook. If
someone uses a copy and then saves it, it'll try to do the save again.

One way is to ask if the backup copy should be made or use some indicator that
will only work for you.

If you set your username under Tools|Options|General tab, you could rely on
that:

You could modify the workbook_beforesave event like this:

Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If UCase(Application.UserName) = LCase("yourusernamehere") Then
Application.OnTime Now + TimeSerial(0, 0, 3), "SaveACopy"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top