?
Solved

Closing out of Excel...

Posted on 2002-03-19
6
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 200 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 101

Expert Comment

by:mlmcc
ID: 6881234
listening
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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