[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Microsoft Word 2003 Cancel close

Posted on 2013-06-02
6
Medium Priority
?
417 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
[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
  • 3
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
New style of hardware planning for Microsoft Exchange server.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
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…

656 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