+ Reply to Thread
Results 1 to 4 of 4

VBS for backup while saving word file

Hybrid View

  1. #1
    Registered User
    Join Date
    11-13-2019
    Location
    Ploiesti
    MS-Off Ver
    2019
    Posts
    4

    VBS for backup while saving word file

    I have an excel that each time I save one more file is saved separately with the name of the respective file, to which is added the date and time of the save in the file name.
    I use the macro below for this.
    The same thing I would like to do for a word file, but I can't.
    Please help.
    Thanks.

    Option Explicit
    
    Private Sub Workbook_Open()
        Application.Caption = "Microsoft Excel AutoBackup"
    End Sub
     
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Debug.Print Split(ThisWorkbook.Name, ".")(1)
        Dim MyFilePath$, Extension$
        MyFilePath = ("C:\Documents\Backup\")
        Extension = Left(ThisWorkbook.Name, Len _
        (ThisWorkbook.Name) - 4) & " Backup"
        
        On Error Resume Next '<< folder exists
        MkDir MyFilePath & Extension '<< create folder
         'save current version of this book in the folder
        ActiveWorkbook.SaveCopyAs Filename:=MyFilePath & _
        Extension & "\" & Extension & _
        (Format(Now, " yyyy-MM-dd, HH.mm.ss.")) & _
        Split(ThisWorkbook.Name, ".")(1)
        
    End Sub
    Last edited by 6StringJazzer; 01-31-2020 at 08:15 AM. Reason: Moderator changed QUOTE tags to CODE tags

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,758

    Re: VBS for backup while saving word file

    It's not clear from your question but I am going to assume you want to do this for a Word file as a Word macro, not an Excel macro.

    One issue is that Excel has a SaveCopy method but Word does not. I have solved this problem in the past by saving the document, saving it with the new name, then re-opening the original. Also, the model in Word for handling a Save event is more complex than how it works in Excel. You can read that Microsoft page for details on capturing the event, then you can call the Sub below from it.

    A simpler option might be to use the Windows file system to make the copy, rather than saving as a copy. I have not tested this but it illustrates the approach.

    Private Sub SaveBackup()
    
        Dim MyFilePath As String, Extension As String
        
        MyFilePath = ("C:\Documents\Backup\")
        
        Extension = Left(ThisDocument.Name, Len(ThisDocument.Name) - 4) & " Backup"
        
        On Error Resume Next '<< folder exists
        MkDir MyFilePath & Extension '<< create folder
         'save current version of this book in the folder
        ActiveDocument.Save ' save first to make sure the copy is the current version
        FileCopy Source:=ActiveDocument.Name, _
                 Destination:=MyFilePath & Extension & "\" & Extension & Format(Now, " yyyy-MM-dd, HH.mm.ss.") & Split(ThisDocument.Name, ".")(1)
    
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-13-2019
    Location
    Ploiesti
    MS-Off Ver
    2019
    Posts
    4

    Re: VBS for backup while saving word file

    Not working

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,758

    Re: VBS for backup while saving word file

    I did test this and it turns out FileCopy will fail if the source file is open. I was able to get this to work. You will need to research how to capture the Save event in the link I provided above.

    Private Sub SaveBackup()
    
        Dim MyFilePath As String, Extension As String
        Dim FS As Object
        Set FS = CreateObject("Scripting.FileSystemObject")
        
        MyFilePath = ("C:\Documents\Backup\")
        
        Extension = Left(ThisDocument.Name, Len(ThisDocument.Name) - 4) & " Backup"
        
        On Error Resume Next '<< folder exists
        MkDir MyFilePath & Extension '<< create folder
         'save current version of this book in the folder
        ActiveDocument.Save ' save first to make sure the copy is the current version
        FS.CopyFile ActiveDocument.Name, _
                         MyFilePath & Extension & "\" & Extension & Format(Now, " yyyy-MM-dd, HH.mm.ss.") & Split(ThisDocument.Name, ".")(1), _
                         True
        Set FS = Nothing
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Code to disable only specific macro when saving backup file
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-06-2016, 11:24 AM
  2. Saving a Backup as a Different File
    By Vlade777 in forum Excel General
    Replies: 2
    Last Post: 01-30-2014, 09:52 AM
  3. Saving Backup Copies before Updateing File
    By kjsconv in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2013, 09:12 PM
  4. Replies: 6
    Last Post: 03-07-2012, 04:56 PM
  5. Replies: 1
    Last Post: 04-19-2006, 11:30 AM
  6. Saving backup file on file open
    By AmyTaylor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2006, 10:48 AM
  7. [SOLVED] Backup:always create a backup file in the save options
    By Alek in forum Excel General
    Replies: 1
    Last Post: 01-07-2006, 09:25 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1