XL: Using Visual Basic to Exit Windows From Within Excel

Last reviewed: September 2, 1997
Article ID: Q110005
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows

SUMMARY

You can exit Windows from within Microsoft Excel 5.0, 7.0 and 97 by using a Visual Basic macro. The procedure below makes a call to a Windows dynamic-link library (DLL) that is similar to clicking Exit on the File menu in Program Manager in Windows 3.1, or clicking Shutdown on the Start menu in Windows 95.

When you use a visual Basic macro to exit Windows, other Windows programs and instances of Microsoft Excel will prompt you to confirm that you want to quit (just as if you had exited Windows manually). If you click Cancel when you are prompted to save a file, the exit request is also canceled

MORE INFORMATION

CAUTION: The Declare, Call and Register Functions listed in the following macro are very sensitive. If used incorrectly, these functions may cause a general protection fault (GP Fault) in Windows or cause other software problems.

NOTE: The macros shown below will not work on systems running Microsoft Windows NT (all versions). Microsoft Windows NT does not allow applications to control the operating system in the manner described below. These macros will run without error but will have no effect.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

The following procedures will allow you to exit Windows from Microsoft Excel:

Microsoft Excel 7.0, 7.0a, and 97

' The following two lines set constant variables that
' will be used by the ExitWindows function.
Const EWX_SHUTDOWN = 1 Const EWX_REBOOT = 2

' Declare API function.
Declare Function ExitWindows Lib "user32" Alias "ExitWindowsEx" _
          (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Sub Exit_Windows()
Dim result As Variant

' The following line is a placeholder.
begin:
' Display a box that returns a choice number to the variable "result".
result = Application.InputBox("Choose One of The Following:" & Chr(13) _
          & Chr(13) & "1. Force Windows to Quit (Shutdown)" & Chr(13) & _
          "2. Reboot The Machine")

' Test the variable "result"
Select Case result
' If "result" is 1 execute the ExitWindows function passing it
' the variable for a Shutdown.
    Case 1
        ExitWindows EWX_SHUTDOWN, &hffff
' If "result" is 2 execute the ExitWindows function passing it
' the variable for a Reboot.
    Case 2
        ExitWindows EWX_REBOOT, &hffff
' If "result" is False the Cancel button was chosen on the
' InputBox so it exits the subroutine.
    Case False
        Exit Sub
' If "result" is anything other than one of the choices above
' the following line displays a message box which either exit the
' subroutine or start at the "begin" line.
    Case Else
        choice = MsgBox("You Didn't Choose a Number." & Chr(13) & _
             Chr(13)& "Please Enter The Number That Corresponds" & _
             Chr(13) & "to Your Choice.", vbOKCancel + vbQuestion)
        If choice = vbOK Then
        GoTo begin
        Else
        Exit Sub
        End If
    End Select
End Sub

Microsoft Excel 5.0 and 5.0c

'The following Declare statement should be entered on a single line
Declare Function ExitWindows Lib "User" (ByVal dwReturnCode As Long,
ByVal wReserved As Integer) As Integer

Sub ExitWin
   Call ExitWindows(1,0)
End Sub

This will cause Windows to close all open applications immediately and return to MS-DOS.

CAUTION: Any sheets that you are editing in the current instance of Microsoft Excel (the instance from which the macro is run) will be closed without confirmation. You will lose any changes made since the last time these files were saved. Other Windows applications and instances of Microsoft Excel prompt you for confirmation (just as if you had exited Windows manually).

REFERENCES

Microsoft Windows SDK Microsoft Visual Basic 3.0 Professional - WINAPI31.HLP help file


Additional query words: 97 8.00 7.00 7.00a 5.00c 5.00 Sdk dynamic link
Keywords : kbprg PgmOthr kbprg
Version : 5.00 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.