Solved

How to find class of excel running...vb6

Posted on 2004-09-24
2
331 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:Mike Eghtebas
  • 2
2 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 12148809
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.
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 12148907
Of course if you have multiple instances of Excel open GetObject will only return a reference to the first one.  Maybe the workbook you are searching for is in another instance of Excel.  So the code above will not work in the case where multiple instances of Excel are open.

Time for a rewrite.

Looking at the documentation on GetObject, it says:

"You can attach to a specific instance if you know the name of an open document in that instance. For example, if an instance of Excel is running with an open workbook named Book2, the following code attaches successfully to that instance even if it is not the earliest instance that was launched:
Set xlApp = GetObject("Book2").Application"

Bingo!

The new function follows.  It will return true if ANY instance of Excel is open with the given Workbook name.

Function IsWorkBookOpen(strWBName As String) As Boolean
   
    Dim oXL As Object
   
    IsWorkBookOpen = False
   
    On Error Resume Next
   
    Set oXL = GetObject(strWBName).Application
    If Not oXL Is Nothing Then IsWorkBookOpen = True
    Set oXL = Nothing
   
   
End Function
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

786 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