Solved

VBA keep word document open

Posted on 2004-03-22
2
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

688 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