Solved

Closing out of Excel...

Posted on 2002-03-19
6
162 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 52

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

738 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