Solved

Microsoft Word 2003 Cancel close

Posted on 2013-06-02
6
396 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 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
Worked fine by me. See the attached sample.

p.s.
Changed code slightly.
EE.zip
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 39

Author Comment

by:thenelson
Comment Utility
That file is for Word 2007+. l am using Word 2003.
0
 
LVL 26

Expert Comment

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

Author Comment

by:thenelson
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

762 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