Solved

Microsoft Word 2003 Cancel close

Posted on 2013-06-02
6
405 Views
Last Modified: 2013-06-04
I have the following code:
Sub AutoClose()
    If Not IsDate(ActiveDocument.Bookmarks("EndTime").Range.Text) And Not booAutoOpenTest Then
        If MsgBox("The end time of this report has not been set." & vbCrLf & "Do you want to set it now?", vbYesNo) = vbYes Then
            ActiveDocument.Saved = False
            SendKeys "{ESC}"
            Selection.GoTo What:=wdGoToBookmark, Name:="EndTime"
        End If
    End If
End Sub

Open in new window

The SendKeys "{ESC}" stops Ms Word 2003 from closing. Without it Word closes.  However Windows 7 does not permit Sendkeys - it throws error 70: permission denied. How can I get around this and prevent Ms Word from closing if yes is answered to the message box?
0
Comment
Question by:thenelson
  • 3
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39215305
If you want it to fire when a document is closed (it wont work if you close Word totally):
1. Create a new module and add the following code to it
Sub DocClose()
    If Not IsDate(ActiveDocument.Bookmarks("EndTime").Range.Text) And Not booAutoOpenTest Then
        If MsgBox("The end time of this report has not been set." & vbCrLf & "Do you want to set it now?", vbYesNo) = vbYes Then
            ActiveDocument.Saved = False
            Exit Sub
            MsgBox "Sorry...I am not going to close the document."
            Selection.GoTo What:=wdGoToBookmark, Name:="EndTime"
        Else
            ActiveDocument.Close
        End If
    End If
End Sub

Open in new window


If you want it to fire when Word is closed:

1. Create a class module naming it oAppClass for example.
2. Add the following code:
Option Explicit

Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    If Not IsDate(ActiveDocument.Bookmarks("EndTime").Range.Text) And Not booAutoOpenTest Then
        If MsgBox("The end time of this report has not been set." & vbCrLf & "Do you want to set it now?", vbYesNo) = vbYes Then
            ActiveDocument.Saved = False
            Cancel = True
            Selection.GoTo What:=wdGoToBookmark, Name:="EndTime"
        End If
    End If
End Sub

Open in new window

3. Create a new module and add the following code to it:
Option Explicit

Dim oAppClass As New oAppClass

Public Sub AutoOpen()
    Set oAppClass.oApp = Word.Application
End Sub

Open in new window

0
 
LVL 39

Author Comment

by:thenelson
ID: 39215396
MacroShadow,

The code you provided for "If you want it to fire when a document is closed..."  Works fine.

 But the code for "If you want it to fire when Word is closed:" does not work. The sub oApp_DocumentBeforeClose  does not fire at all.  I placed a non conditional Message box in it and it did not fire.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39215413
Worked fine by me. See the attached sample.

p.s.
Changed code slightly.
EE.zip
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 39

Author Comment

by:thenelson
ID: 39216030
That file is for Word 2007+. l am using Word 2003.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39216035
Sorry, I don't have 2003 anymore. Check the attached file.
EE.doc
0
 
LVL 39

Author Comment

by:thenelson
ID: 39220634
Your EE.doc file worked exactly as expected in Word 2003.

However when I copied the code to my Word doc, l got  a popup that stated: "You cannot close Microsoft Office Word because a dialog box is open...." I got rid of that popup by removing the line:
oApp.Application.Quit ' .Close

My final subroutine in the class module became:
Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Dim i As Integer
    If Not IsDate(ActiveDocument.Bookmarks("EndTime").Range.Text) And Not booAutoOpenTest Then
        If MsgBox("The end time of this report has not been set." & vbCrLf & "Do you want to set it now?", vbYesNo) = vbYes Then
            Cancel = True
            Selection.GoTo What:=wdGoToBookmark, Name:="EndTime"
        End If
    End If
End Sub

Open in new window

So if the DocumentBeforeClose event is not cancelled, the close happens without the quit line.

 Thanks for your help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

735 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