[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Closing out of Excel...

Posted on 2002-03-19
6
Medium Priority
?
166 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
Industry Leaders: 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 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