?
Solved

VBA keep word document open

Posted on 2004-03-22
2
Medium Priority
?
427 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:mnorma12
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 375 total points
ID: 10652722
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()
Register_Event_Handler
End Sub
Sub AutoNew()
Register_Event_Handler
End Sub
Sub Register_Event_Handler()
    Set mdlEvClass.App = Word.Application
End Sub
Sub Autoexec()
Register_Event_Handler
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

0
 

Author Comment

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

Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question