• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

Microsoft Word 2003 Cancel close

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
thenelson
Asked:
thenelson
  • 3
  • 3
1 Solution
 
MacroShadowCommented:
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
 
thenelsonAuthor Commented:
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
 
MacroShadowCommented:
Worked fine by me. See the attached sample.

p.s.
Changed code slightly.
EE.zip
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
thenelsonAuthor Commented:
That file is for Word 2007+. l am using Word 2003.
0
 
MacroShadowCommented:
Sorry, I don't have 2003 anymore. Check the attached file.
EE.doc
0
 
thenelsonAuthor Commented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now