Hi all,
I've been stumbling around with this for awhile now. I was wanting some code that could
tell me if a workbook is open. However, I am wanting it to be able to check for the workbook within another instance of a excel app if running in a different thread.
I searched the internet for some exsisting code and could only find the following that looked promising.
http://www.eggheadcafe.com/microsoft...xcel-open.aspx
However, after inserting the code and making a few tweaks to the declarations.
I'm now stumbeling on the following line of code .
Call GetClassName(hWin, sBuff, 7)
sBuff, appears to be empty, I'm not sure what sBuff is suppose to hold.
Here's the full code I'm trying to use.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" ( _
ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" ( _
ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Sub Test()
MsgBox ExcelCount
End Sub
Function ExcelCount() As Long
'http://www.eggheadcafe.com/microsoft/Excel-Programming/31869454/determine-instances-of-excel-open.aspx
Dim hWin As Long
Dim nXLinsts As Long
Dim sBuff As String '* 7
Const CXL As String = "XLMAIN"
hWin = FindWindow(CXL, vbNullString) ' normally incl app.caption
hWin = GetWindow(hWin, GW_HWNDFIRST)
Do
hWin = GetWindow(hWin, GW_HWNDNEXT)
Call GetClassName(hWin, sBuff, 7)
If Left$(UCase$(sBuff), 6) = CXL Then
nXLinsts = nXLinsts + 1
End If
Loop Until hWin = 0
ExcelCount = nXLinsts
End Function
Any help is appreciated.
Thanks,
BDB
Bookmarks