Solved

Closing out of Excel...

Posted on 2002-03-19
6
147 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 49

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now