Solved

How to find class of excel running...vb6

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now