View Full Version : Document Close

11-10-2009, 08:17 AM
I found this code on the internet:

Sub FileClose()


End Sub

Sub DocClose()


End Sub

Sub NewCloseDialog()

On Error GoTo Error

If ActiveDocument.Saved = False Then



End If

Exit Sub


MsgBox ("Something went wrong.")

End Sub

This is really handy. But it only works when you click on: Office-Button>Close. How can I add a function to it that it always happens when you try to close the file????
P.S. I am a noob, so I need well explanation... Not: Oh, you need to add an close.Dialog If function on the third acudelir..:bug:

11-10-2009, 09:41 AM
Is this for MS Word?

Did you look at the document close procedure in the thisdocument module?

11-10-2009, 01:19 PM
Yes this is made for MS Word 2003, and used (by me) for MS Word 2007.

And no, I did not look at the document close procedure in the thisdocument module, because I do not know what that is....

11-10-2009, 02:03 PM
In the vbe look for the thisdocument module in the project explorer and put your code in the document close procedure.

11-11-2009, 08:47 AM
which code???????????
where then exactly????

11-11-2009, 09:10 AM
You know how to find the visual basic editor or vbe

a shortcut key combination to get you there is to hit the alt key and f11.

On the left side of the vbe look for your project explorer. If it's not visible go to view and select project explorer.

In the project explorer look for the line that says thisdocument. Doulbe click it to see the code module on the right....

there are two drop downs at the top of the module. Start with the one on the left and select document from the drop down.

Then on the right drop down select "close"

that is where you put the code you want to run when your document is closed.

11-11-2009, 02:01 PM
Yes but this doesnt work when you press AltF4 or [X]...

11-12-2009, 09:18 AM
(sorry for double post)
It worked!
Really thanks!

11-12-2009, 09:31 AM
You're welcome. Be sure to mark your thread solved using the thread tools at the top of the page.

You will still be able to post followup questions on this thread.