Solved

Microsoft Word 2003 Cancel close

Posted on 2013-06-02
6
400 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Outlook Free & Paid Tools
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…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

785 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