Unique Backup Filename Redux

A

AA

Sub xBackupAndSave()
'copies the OriginalFile to OriginalFile.baq
'then saves the Edited file
On Error Resume Next
Dim strFileA, strFileB
Dim fs As Scripting.FileSystemObject
Set fs = New Scripting.FileSystemObject
strFileA = ActiveDocument.FullName
strFileB = strFileA & ".baq"
fs.CopyFile strFileA, strFileB
With Options
.CreateBackup = False
End With
ActiveDocument.Save
With Options
.CreateBackup = True
End With
End Sub

My Backup macro has been working well.

I put in "On Error Resume Next" so I could use it to save a new
document without it stopping at "fs.CopyFile strFileA, strFileB"
because strFileA doesn't exist yet. (I'd welcome a less crude method).

I didn't need an extra "Backup of FirstPartOfFilename.wbk" so I turned
that off just before saving the current file and then turned it back
on so I have a backup if I use the regular Save command.

I need one further enhancement:

Is there a way for VBA to test if the current file has been edited
after it's been opened so I can exit the macro if the file hasn't been
changed?

If I haven't changed the file and I run the macro, it still copies
strFileA to strFileB, giving me a backup file identical to the current
file, so I lose an iteration unnecessarily.

Andy
 
A

AA

Is there a way for VBA to test if the current file has been edited
after it's been opened so I can exit the macro if the file hasn't been
changed?

Never mind! I figured it out, for a change something was easy to find
in Help, and simple to implement:

If ActiveDocument.Saved = False Then fs.CopyFile strFileA, strFileB

===================

Sub xBackupAndSave()
On Error Resume Next
Dim strFileA, strFileB
Dim fs As Scripting.FileSystemObject
Set fs = New Scripting.FileSystemObject
strFileA = ActiveDocument.FullName
strFileB = strFileA & ".baq"
If ActiveDocument.Saved = False Then fs.CopyFile strFileA, strFileB
Options.CreateBackup = False
ActiveDocument.Save
Options.CreateBackup = True
End Sub

===================

So now if I run the macro but haven't made any changes, nothing happens.

Andy
 
C

Chad DeMeyer

One way to test whether or not a document is new is with
ActiveDocument.Path. If the document is new (e.g., Document1, Document2,
etc.), ActiveDocument.Path = "" (empty string).

Regards,
Chad
 
A

AA

One way to test whether or not a document is new is with
ActiveDocument.Path. If the document is new (e.g., Document1, Document2,
etc.), ActiveDocument.Path = "" (empty string).

Thanks Chad,

Andy
 

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

Top