Solved

How to find class of excel running...vb6

Posted on 2004-09-24
2
335 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

765 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