PDA

View Full Version : Modified Close Procedure



clhare
11-30-2009, 09:31 AM
I am trying to do a macro that runs when the user tries to close the active document (after running a particular template). I want it to check and see if the document created by the template has already been saved and if so, to close it without saving any changes and then delete it.

Here's what I have so far. I get an error when it gets to the line that tries to close the file (in red below) and I have no idea why that's causing a problem. If it hasn't been saved yet, I just want to close it.

Private Sub Document_Close()
Dim strFileToDelete As String
' If the active file is a 'document' file
If ActiveDocument.Type = wdTypeDocument Then
' If the file has been previously saved
If Len(ActiveDocument.Path) <> 0 Then
' Get path and filename
strFileToDelete = ActiveDocument.FullName
' Closes the document without saving it
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Delete the saved file
Kill strFileToDelete
Else
' If it hasn't been saved, close the open document without saving the changes
ActiveDocument.Close SaveChanges:=False
End If
End If
End Sub

Any help is greatly appreciated!

Tinbendr
11-30-2009, 09:59 AM
ActiveDocument.Close SaveChanges:=False

or

ActiveDocument.Close wdDoNotSaveChanges

fumei
11-30-2009, 11:14 AM
Where is this Document_Close procedure?

If the document is not saved yet, how would this procedure even BE there?