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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save on exit code 1

Status
Not open for further replies.

mscallisto

Technical User
Jun 14, 2001
2,990
US
How do I make this macro execute when I close word?
Essentially I want to create a copy of my .doc file
when I'm done for the day.

I don't wish to use "always create backup copy" option.


Sub savewhendone()
ActiveDocument.SaveAs FileName:="BackupOfWord.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
 
Hello,

1. Open up the VB Project editor by hitting Alt+F11 or selecting it from your Tools, Macro, Visual Basic Editor.

2. Ensure the Project Explorer is open. If not, select it from View, Project Explorer or Ctl+R.

3. Open up the folder named Microsoft Word Objects

4. Double Click ThisDocument or right click the icon and select View Code.

5. From the Drop Down List Box in the upper left hand corner, select Document.

6. From the Drop Down List Box in the upper right hand corner, select Close.

7. Drop your code into the Document_Close procedure code that is created for you.

8. If other empty procedure code declarations are created for you, you may freely delete them (eg Document_Open).

Good Luck!

 
One last Question on the same topic.

After I added the macro it worked great just as you said.

However it also works for every new word document when I only want it to apply to certain doc's.

If I delete my normal.dot everything is as it was.

How can I make this macro apply only to doc's I choose?

 
Add this code in the beginning of your procedure code:

Dim lngAnswer As Long

If vbNo = MsgBox("Create Backup for this file", vbYesNo)
Then
Exit Sub
End If

You may have to dig around for enumerated constants in Word. The ones above are from Access. If they don't work look up the MsgBox function and see if they can point you.

Sorry I don't have time to give you working code.

Have a great day!



 
If you put the following into Normal.dot you can get it to ASK if you want a back up copy. It is true it will ask for all files being closed. If you want, you can put this in selected documents then those docs will ask if you want a backup.

If it is specific docs you want to do backup, then remove the question and simply put the backup code in the AutoClose
sub.

Sub AutoClose()
Dim filenamepath As String
Dim myfilename As String
Dim folder As String
Dim strDocName As String
Dim pathlength, namelength As Integer
Dim MakeBackUp

MakeBackUp = MsgBox("Create Back Up File?", vbYesNo)

If MakeBackUp = vbNo Then
Exit Sub
' regular Word question on saving changed file loads
' if you want to bypass this and automatically and save any changes
' put the following BEFORE the Exit Sub
' ActiveDocument.Save (wdSaveChanges)
Else
strDocName = "BackupOfWord.doc"
' the following just makes sure the file is saved
' explicitly to the same folder
myfilename = ActiveDocument.Name
filenamepath = ActiveDocument.FullName
namelength = Len(myfilename)
pathlength = Len(filenamepath)
folder = Left(filenamepath, (pathlength - namelength))

' save file with new name
ActiveDocument.SaveAs FileName:=folder & "\" & strDocName
End If
End Sub
 
Thanks to All.

Both suggestions worked equally as well (for a while)

Now I get the message:
[ The macros in this project are disabled]

This happened to me once a while back and I can't remember how to enable the macro.

 
Now I remember why the AutoClose() macro was disabled.

Tools > Macro > Security
Choose Medium instead of high
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top