Solved

VBA keep word document open

Posted on 2004-03-22
2
414 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 125 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now