Solved

How to find class of excel running...vb6

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 135
Set email body to html using vbscript 6 69
Excel Automation VBA 19 101
transition to visual .net from vb6 5 57
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…

679 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