Consulting

Results 1 to 7 of 7

Thread: Solved: Create backup to another location

  1. #1
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location

    Solved: Create backup to another location

    Hi
    Some time ago, I found this code in the web (dont know the author, sorry) to create backup files.

    It works almost as I wish. I've changed it to create the ".bak" file when workbook open, to automatize the procedure whenever I open the file. However the backups are created in same location of the originals, and I want them stored in another folder, say i.e., "C:\Backups\". But I donīt know where in the code I put that piece.

    Other thing, to automatically create .bak files when workbook is closing, sall I use WorkbookBeforeClose Method?

    [VBA]Here's the code

    Private Sub Workbook_Open() 'Fazer backup
    Dim awb As Workbook, BackupFileName As String, i As Integer, OK As Boolean
    If TypeName(ActiveWorkbook) = "Nothing" Then Exit Sub
    Set awb = ActiveWorkbook
    If awb.Path = "" Then
    Application.Dialogs(xlDialogSaveAs).Show
    Else
    BackupFileName = awb.FullName
    i = 0
    While InStr(i + 1, BackupFileName, ".") > 0
    i = InStr(i + 1, BackupFileName, ".")
    Wend
    If i > 0 Then BackupFileName = Left(BackupFileName, i - 1)
    BackupFileName = BackupFileName & ".bak"
    OK = False
    On Error GoTo NotAbleToSave
    With awb
    Application.StatusBar = "Saving this workbook..."
    .Save
    Application.StatusBar = "Saving this workbook backup..."
    .SaveCopyAs BackupFileName
    OK = True
    End With
    End If
    NotAbleToSave:
    Set awb = Nothing
    Application.StatusBar = False
    If Not OK Then
    MsgBox "Backup Copy Not Saved!", vbExclamation, ThisWorkbook.Name
    End If
    End Sub[/VBA]

    Thanks in advance for your precious help.
    Ioncila

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I don't understand why you are saving as a .bak file but this looks easier and gives you some options for filname in case you save the same workbook twice......

    [vba]Option Explicit
    Sub SvMe() 'Save filename as value of A1 plus the current date
    Dim newFile As String, fName As String
    ' Don't use "/" in date, invalid syntax
    'fName = Range("A1").Value
    newFile = ActiveWorkbook.Name
    ' newFile = fName & " " & Format$(Date, "mm-dd-yyyy")
    ' Change directory to suit
    ChDir _
    "C:\Temp"
    ActiveWorkbook.SaveCopyAs Filename:=newFile
    ' ActiveWorkbook.SaveCopyAs Filename:=newFile & ".xls"
    End Sub[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    Thank you for your help

    Your solution is much better than mine.

    A little change to automatize the copy procedure, I had do this (underline):

    [VBA]Option Explicit
    Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Save filename as value of A1 plus the current date
    Dim newFile As String, fName As String
    ' Don't use "/" in date, invalid syntax
    'fName = Range("A1").Value
    newFile = ActiveWorkbook.Name
    ' newFile = fName & " " & Format$(Date, "mm-dd-yyyy")
    ' Change directory to suit
    ChDir _
    "C:\Testes"
    ActiveWorkbook.SaveCopyAs Filename:=newFile
    ' ActiveWorkbook.SaveCopyAs Filename:=newFile & ".xls"
    End Sub[/VBA]

    And that works as wish, but if I use the copy file, it gives an error in the bold line.
    That is not quiet important because the copy file will be used in an emergency, but is there a way to prevent that?

    Thanks

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    It works just fine for me. does the directory C:\Testes exist and is the original file saved so it actually has a name?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    Quote Originally Posted by lucas
    It works just fine for me. does the directory C:\Testes exist and is the original file saved so it actually has a name?
    I did the following:

    As an experience, I saved original (Testbackup.xls) in desktop and created "C:\Testes to store the copy one.

    The copy file has the same name of the original.

    Like I said, copy procedure works fine, like I want. Then If I open the copy file, and stiil it's allright. But when I close it, that brings an error on that bold code line with msg "impossible to access to Testbackup.xls". I think the error appears baecause macro still exist in the copyfile and it can't find the path.
    Like I said, that is not important because the important function is done (create a backup copy).

    One small question:
    shouldn't the copy file name appear with date, like your code means?

    Thanks

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    The reason you get an error is because you are opening it in the directory that it needs to save to, ie C:\Testes and that named file is already in existance there.

    If you move it out of that directory, it will run again.

    That's why you need to add the date or somthing so the file name won't be the same each day.
    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Save filename as value of A1 plus the current date
    Dim newFile As String, fName As String
    newFile = ActiveWorkbook.Name
    ChDir _
    "C:\Temp"
    ActiveWorkbook.SaveCopyAs Filename:=newFile & " " & Format$(Date, "mm-dd-yyyy") & ".xls"
    End Sub[/VBA]

    It might be better not to have it in the close event and just have a button to save it unless you are sure you will only be opening it once a day and want to save a copy.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    That suits my needs

    Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •