By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

VBA to close an HIDDEN occurence of Excel

P: n/a
Hi

I have a routine that uses VBA to open a hidden occurence of Excel,
and do background computations.

However, in the event that the routine terminates abnormally, I have
not been able to find a way of closing this Hidden occurrence. I can
close any foreground instances without any problem, but the same
coding does not work for hidden occurences.

Code as below:-
Function fIsAppRunning(ByVal strAppName As String, _
Optional fActivate As Boolean) As Boolean
Dim lngH As Long, strClassName As String
Dim lngX As Long, lngTmp As Long
Const WM_USER = 1024
On Local Error GoTo fIsAppRunning_Err
fIsAppRunning = False
Select Case LCase$(strAppName)
Case "excel": strClassName = "XLMain"
Case Else: strClassName = vbNullString
End Select

If strClassName = "" Then
lngH = apiFindWindow2(vbNullString, strAppName)
Else
lngH = apiFindWindow2(strClassName, vbNullString)
End If
If lngH <> 0 Then
apiSendMessage lngH, WM_USER + 18, 0, 0
lngX = apiIsIconic(lngH)
If lngX <> 0 Then
lngTmp = apiShowWindow(lngH, SW_SHOWNORMAL)
End If
If fActivate Then
lngTmp = apiSetForegroundWindow(lngH)
End If
fIsAppRunning = True
End If
fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
Function fCloseApp(lpClassName As String) As Boolean

Dim lngRet As Long, hWnd As Long, pID As Long

hWnd = apiFindWindow(lpClassName, vbNullString)
If (hWnd) Then
lngRet = apiPostMessage(hWnd, WM_CLOSE, 0, ByVal 0&)
Call apiGetWindowThreadProcessId(hWnd, pID)
Call apiWaitForSingleObject(pID, INFINITE)
fCloseApp = Not (apiIsWindow(hWnd) = 0)
End If
End Function

And then, to close the running instance in my process -

Do While fIsAppRunning("excel")
Set xlApp = GetObject(, "Excel.Application")
If xlApp.Visible = True Then
fCloseApp ("XLMain")
End If
Set xlApp = Nothing
Loop

Any help would be appreciated

Thanks

Gary
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi

Hi
If you can close all open refs to objects in the excel
including the application object it should finish.
The code you use, and any variant which closes the hidden
excel, will likely give memory leaks and close other
instances of excel which might be running.

You can pick up excel which is already running using code
like

Const ERR_APP_NOTRUNNING As Long = 429
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err = ERR_APP_NOTRUNNING Then
Set xlApp = New Excel.Application
End If

If using excel 2002 or earlier and closing excel manually
you should look at
http://support.microsoft.com/kb/q187745/
if not already seen

-David
On 3 Dec 2004 00:44:15 -0800, ga*********@nhs.net (Gary Cobden) wrote:
Hi

I have a routine that uses VBA to open a hidden occurence of Excel,
and do background computations.

However, in the event that the routine terminates abnormally, I have
not been able to find a way of closing this Hidden occurrence. I can
close any foreground instances without any problem, but the same
coding does not work for hidden occurences.

Code as below:-
Function fIsAppRunning(ByVal strAppName As String, _
Optional fActivate As Boolean) As Boolean
Dim lngH As Long, strClassName As String
Dim lngX As Long, lngTmp As Long
Const WM_USER = 1024
On Local Error GoTo fIsAppRunning_Err
fIsAppRunning = False
Select Case LCase$(strAppName)
Case "excel": strClassName = "XLMain"
Case Else: strClassName = vbNullString
End Select

If strClassName = "" Then
lngH = apiFindWindow2(vbNullString, strAppName)
Else
lngH = apiFindWindow2(strClassName, vbNullString)
End If
If lngH <> 0 Then
apiSendMessage lngH, WM_USER + 18, 0, 0
lngX = apiIsIconic(lngH)
If lngX <> 0 Then
lngTmp = apiShowWindow(lngH, SW_SHOWNORMAL)
End If
If fActivate Then
lngTmp = apiSetForegroundWindow(lngH)
End If
fIsAppRunning = True
End If
fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
Function fCloseApp(lpClassName As String) As Boolean

Dim lngRet As Long, hWnd As Long, pID As Long

hWnd = apiFindWindow(lpClassName, vbNullString)
If (hWnd) Then
lngRet = apiPostMessage(hWnd, WM_CLOSE, 0, ByVal 0&)
Call apiGetWindowThreadProcessId(hWnd, pID)
Call apiWaitForSingleObject(pID, INFINITE)
fCloseApp = Not (apiIsWindow(hWnd) = 0)
End If
End Function

And then, to close the running instance in my process -

Do While fIsAppRunning("excel")
Set xlApp = GetObject(, "Excel.Application")
If xlApp.Visible = True Then
fCloseApp ("XLMain")
End If
Set xlApp = Nothing
Loop

Any help would be appreciated

Thanks

Gary


Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.