Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

How to find class of excel running...vb6

I have:

Declare Function GetDesktopWindow Lib"user32" () As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
 (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
'--------------------------------------------------------------------------
Function IsWorkBookOpen(strWBName As String) As Boolean

        Dim dWnd As Long, hWnd As Long, mWnd As Long, cWnd As Long
        dWnd = GetDesktopWindow
        hWnd = FindWindowEx(dWnd, 0&,"XLMAIN", vbNullString)
        mWnd = FindWindowEx(hWnd, 0&,"XLDESK", vbNullString)
        While mWnd <> 0 And cWnd = 0

                cWnd = FindWindowEx(mWnd, 0&,"EXCEL7", strWBName)     '<---  using "EXCEL7" for class

                hWnd = FindWindowEx(dWnd, hWnd,"XLMAIN", vbNullString)
                mWnd = FindWindowEx(hWnd, 0&,"XLDESK", vbNullString)
        Wend
        If cWnd > 0 Then
                IsWorkBookOpen = True
        End If

End Function
'--------------------------
I want to replace above line with:

Dim strExcelClass As String
strExcelClass = <need help here to determine excel class>
cWnd = FindWindowEx(mWnd, 0&,strExcelClass , strWBName)     '<---  and use strExcelClass  in place of "EXCEL7"

Do you know what is the syntax for it.

Thanks.
Avatar of Erick37
Erick37
Flag of United States of America image

As an alternative to using the API to search for a workbook, you could use automation to search the open workbooks in Excel as follows:

Function IsWorkBookOpen(strWBName As String) As Boolean

    Dim oXL As Object
    Dim oXlWb As Object
   
    IsWorkBookOpen = False
   
    On Error Resume Next
    'Get open excel application
    Set oXL = GetObject(, "excel.application")
   
    'Search each workbook name
    For Each oXlWb In oXL.Workbooks
        Debug.Print oXlWb.Name
        If (UCase(oXlWb.Name) = UCase(strWBName)) Then
            IsWorkBookOpen = True
            Exit For
        End If
    Next
   
    Set oXlWb = Nothing
    Set oXL = Nothing

End Function

Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial