Excel

Collapse Excel inwards and exit

Ease of Use

Easy

Version tested with

2000, 2002, 2003 

Submitted by:

Marcster

Description:

This example will slowly collapse inwards Excel's main window and quit. 

Discussion:

This example will exit Excel differently to the usual default way. It will exit Excel in a more impressive way by animating it to collapse inwards till it disappears. This uses the API function AnimateWindow to do this. You could use this as a novel way to exit out of an Excel workbook. 

Code:

instructions for use

			

Option Explicit Private Declare Function AnimateWindow Lib "USER32" (ByVal hWnd As Long, ByVal _ dwTime As Long, ByVal dwFlags As Long) As Boolean Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal _ lpClassName As String, ByVal lpWindowName As String) As Long Const AW_CENTER = &H10 'Collapse inward Const AW_HIDE = &H10000 'Hide Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim lngFrmWindow As Long 'Retrieve a handle to Excel 'XLAMIN is the Class name for Excel 2000/2002 'Application.Name & " - " & ActiveWorkbook.Name 'retrieves Excel's window title text, this is what we need lngFrmWindow = FindWindow("XLMAIN", Application.Name & " - " & ActiveWorkbook.Name) If lngFrmWindow <> 0 Then 'lngFrmWindow returns a zero if there's an error 'Make Excel Collapse inwards and exit '750 is in milliseconds the time length of the animation AnimateWindow lngFrmWindow, 750, AW_CENTER Or AW_HIDE Else 'An error has occured with the AnimateWindow function Call MsgBox("There was an error when trying to animate the Excel window." _ & vbCrLf & "" _ , vbCritical, Application.Name) End If 'Quit Excel Application.Quit End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Paste the code from above into the ThisWorkBook module.
  4. Close the VBE (Alt + Q or press the X in the top right corner).
  5. Exit Excel.
 

Test the code:

  1. Open the attached workbook.
  2. Exit the workbook or Excel.
 

Sample File:

CollapseExcel.zip 7.64KB 

Approved by mdmackillop


This entry has been viewed 325 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express