Using Visual Basic to Exit Windows From Within Excel (110005)
The information in this article applies to:
- Microsoft Excel 97 for Windows
This article was previously published under Q110005 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 INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
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.
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
Modification Type: | Minor | Last Reviewed: | 10/10/2006 |
---|
Keywords: | kbProgramming KB110005 |
---|
|