VBA keep word document open

I need some code for a MS Word macro that will keep a document from closing until certain fields are filled out. I can trap the field state (clean/dirty) I just need a way to keep word from closing. This has to work in any circumstances. Whether the user closes just the active document or closes word all together. Also needs to work in Word 2000, XP, and 2003.
Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
This works in Word 2000. I haven't got the other two

In a module in you template, put this code:

Dim mdlEvClass As New Class1
Sub AutoOpen()
End Sub
Sub AutoNew()
End Sub
Sub Register_Event_Handler()
    Set mdlEvClass.App = Word.Application
End Sub
Sub Autoexec()
End Sub

Put this code in a class module called 'Class1'

Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
'Replace the MsgBox question with your document test
If MsgBox("Do you want to close this document", vbYesNo) = vbNo Then
    Cancel = True
End If
End Sub

mnorma12Author Commented:
This works well. Maybe too well. Once I get this in there i can no longer close my template. I'll work around it.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.