Save a backup copy on closing

H

HONYAKUKA

Word2000
WinXP

I recently had a minor disaster in which I must have sent an important file to
the Recycle Bin with some other stuff and then emptied the Bin, forever losing
the file. (I downloaded a sample recovery program, but it showed all sorts of
ancient files, but not the one I had just trashed.)

Anyway, does anyone have a few good lines of code to save a backup copy of a
Word file in a separate folder, as part of the closing or Quit routine?

I know the basic code for Saving As, etc. What I'm wondering is, Is there a
way to put such code in an AutoClose or Close Event module in a way that will
save the backup and save the file itself correctly and smoothly?

Here's the scenario: You have a Save As routine in your AutoClose module for
the backup version. You make some changes in the file you're about to close,
but don't save them. You close the file. The AutoClose routine runs, Saving
the file As another name. But this prevents the final prompt to save changes
in the FILE ITSELF from appearing. Thus, your backup has the latest changes to
your file, but your file itself doesn't.

At the beginning of the AutoClose module, I could simply add a line of code to
Save the file. But I'd rather be prompted to do so, just in case.

Thanks for any help.

Jay
 
P

Perry

The savest way is to intercept the Close event of Word as an application.
You wud need to raise the eventhandler up the application level.

Here are some articles on how to do that.
Look at the DocumentBeforeClose event.
Below there's another sample.

Krgrds,
Perry


http://msdn.microsoft.com/library/d...en-us/vbawd10/html/wohowApplicationEvents.asp

http://www.mvps.org/word/FAQs/MacrosVBA/AppClassEvents.htm

'*** sample ***
Private Sub app_DocumentBeforeClose(ByVal Doc As Document, _
Cancel As Boolean)
'you cud test whether this applies to specific templates
'like in:
If Doc.AttachedTemplate.Name = _
"A_SpecificTemplate.dot" Then

'Cancel results in True when you want to save
Cancel = (MsgBox("Save first?", vbYesNo) = vbYes)
End If
End Sub
 
M

Mads

HONYAKUKA said:
I know the basic code for Saving As, etc. What I'm wondering is, Is there a
way to put such code in an AutoClose or Close Event module in a way that will
save the backup and save the file itself correctly and smoothly?
As I understand you wish to have a copy of your file. I suggest that you use
something like "WordBasic.CopyFileA"
Here's the scenario: You have a Save As routine in your AutoClose module for
the backup version. You make some changes in the file you're about to close,
but don't save them. You close the file. The AutoClose routine runs, Saving
the file As another name. But this prevents the final prompt to save changes
in the FILE ITSELF from appearing. Thus, your backup has the latest changes to
your file, but your file itself doesn't.

At the beginning of the AutoClose module, I could simply add a line of code to
Save the file. But I'd rather be prompted to do so, just in case.

Perhaps you can use this example.

Private Sub Document_Close()
If ActiveDocument.Saved Then
WordBasic.CopyFileA FileName:=ActiveDocument.FullName,
Directory:="c:\BackupDir\" & ActiveDocument.Name
Else
Application.Dialogs(wdDialogFileSaveAs).Show
ActiveDocument.Saved = True
WordBasic.CopyFileA FileName:=ActiveDocument.FullName,
Directory:="c:\BackupDir\" & ActiveDocument.Name
End If
End Sub

I use the SaveAs dialog to let the user decide if the document should be
saved or not.

Best regards
Mads Knudsen
 

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