Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to Autosave and Backup Active Word Files

Status
Not open for further replies.

sam22AA

Technical User
Jul 31, 2011
1
DE
Hi,

I am an absolute beginner in VBA, and have stumbled across it because of G. Mayor's addin for auto saving active word files. (
The add-in at that site can auto-save open word files at a user defined interval. Another add-in he provides, can backup the current file to a user specified location. However, I am interested in a macro, which will not only auto-save, but also immediately backup the file with a date/time stamp in a user-defined folder.

I have been trying to create a macro but have been unsuccesful in getting it to work, and was wondering it the more experienced users here can help me out.

This is the macro, which I have created, and which does not work:

Sub FileBackupSave()

Dim strDocName As String
Dim strWordBackupDoc As String
Dim strMyBackupDoc As String

Options.CreateBackup = True

ActiveDocument.Save
ActiveDocument.Saved = False
ActiveDocument.Save

strDocName = ActiveDocument.FullName

strWordBackupDoc = ActiveDocument.Path & "\Backup of " & ActiveDocument.FullName

strMyBackupDoc = ActiveDocument.Path & "\Backup of " & Format(Date, "yyyy-mm-dd") & _
" " & Format(Time, "hh-mm-ss AMPM") & ActiveDocument.FullName

FileCopy strWordBackupDoc, strMyBackupDoc
End Sub

Thanks,

Sam
 
Autosaving an open document could be a bad idea - what if the user has just made a mistake and made changes that he wants to undo? Why not prompt the user instead and give the option to autosave? (Use ontime and msgbox)

Fullname includes the path. You just want Name.
Code:
strWordBackupDoc = ActiveDocument.Path & "\Backup of " & ActiveDocument.[red]Full[/red]Name
strMyBackupDoc = ActiveDocument.Path & "\Backup of " & Format(Date, "yyyy-mm-dd") & _
    " " & Format(Time, "hh-mm-ss AMPM") & ActiveDocument.[red]Full[/red]
Finally, the backup copy uses a different file extension so for strMyBackupDoc you need to manipulate Name to strip out .DOC and replace with .wbk. Have a look at the Left and Len functions for this.

Post back with any issues.

There are probably better approaches to what you are trying to do (eg using the BeforeSave event and the FSO)

Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top