Solved

Closing out of Excel...

Posted on 2002-03-19
6
154 Views
Last Modified: 2010-05-02
I put in a Close & Exit routine in my Visual Basic Program.  It does not want to close all the way out of the program.  Cause when I look into the tasklist on the things that are still running is EXCEL.exe

This is the code that I am using...


    Set objWorksheet = Nothing
    objWorkbook.Close False
    Set objWorkbook = Nothing
    objExcel.Quit
    Set objExcel = Nothing

John-
0
Comment
Question by:vbjohn
6 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 50 total points
ID: 6881060
This will check to see if Excel is running.
Add these API's and procedure to your project...

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


 Call DetectExcel

Public Sub DetectExcel()
  Const WM_CLOSE = &HF060&
  Const WM_SYSCOMMAND = &H112
  Dim hwnd As Long

'Class Name         Application
'SciCalc CALC.EXE
'CalWndMain Calendar.EXE
'CARDFILE CARDFILE.EXE
'Clipboard Clipboard.EXE
'CLOCK CLOCK.EXE
'CtlPanelClass Control.EXE
'XLMain EXCEL.EXE
'Session MS - DOS.EXE
'NOTEPAD NOTEPAD.EXE
'pbParent PBRUSH.EXE
'Pif PIFEDIT.EXE
'PrintManager PRINTMAN.EXE
'Progman            PROGMAN.EXE   (Windows Program manager)
'RECORDER RECORDER.EXE
'REVERSI REVERSI.EXE
'#32770             SETUP.EXE
'Solitaire SOL.EXE
'TERMINAL TERMINAL.EXE
'WFS_Frame WINFILE.EXE
'MW_WINHELP WINHELP.EXE
'#32770             WINVER.EXE
'OpusApp WINWORD.EXE
'MSWRITE_MENU       WRITE.EXE


hwnd = FindWindow("XLMain", "Microsoft Excel")
  'if Excel is not running this API call returns its handle hwnd = 0
   If hwnd = 0 Then
      Exit Sub
   Else
      'if Excel is running this API call will close the App when hwnd > 0
      SendMessage hwnd, WM_SYSCOMMAND, WM_CLOSE, 0
   End If
End Sub
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6881123
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 6881234
listening
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6881567
Maybe:

Set objWorksheet = Nothing
   objWorkbook.Close False
DoEvents
   Set objWorkbook = Nothing
   objExcel.Quit
   Set objExcel = Nothing

0
 
LVL 44

Expert Comment

by:bruintje
ID: 6881781
well if above doesn't help i always walk it down as i build it up like

you can also use early binding like

Dim objExcel As New excel.Application
Dim objWorkBook As excel.Workbook
Dim objWorkSheet As excel.Worksheet

Set objWorkBook = objExcel.Workbooks.Open(FileName:=gsVariableNameXLS)
Set objWorkSheet = objWorkBook.ActiveSheet

'do your processing and close out

'first close all things normally
objWorkbook.Close False
objExcel.Quit
'then cleanup the objects
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

:O)Bruintje
0
 

Author Comment

by:vbjohn
ID: 6882578
Thanks!!!!  It works like a charm.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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