Results 1 to 7 of 7

Determine Number of Instances of Excel Running.

Threaded View

  1. #1
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Determine Number of Instances of Excel Running.

    Hi all,

    I've been stumbling around with this for awhile now. I was wanting some code that could
    tell me if a workbook is open. However, I am wanting it to be able to check for the workbook within another instance of a excel app if running in a different thread.

    I searched the internet for some exsisting code and could only find the following that looked promising.

    http://www.eggheadcafe.com/microsoft...xcel-open.aspx

    However, after inserting the code and making a few tweaks to the declarations.
    I'm now stumbeling on the following line of code .

    Call GetClassName(hWin, sBuff, 7)
    sBuff, appears to be empty, I'm not sure what sBuff is suppose to hold.

    Here's the full code I'm trying to use.

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function GetWindow Lib "user32" ( _
    ByVal hWnd As Long, ByVal wCmd As Long) As Long
    
    Private Declare Function GetClassName Lib "user32" ( _
    ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Const GW_HWNDFIRST = 0
    Private Const GW_HWNDNEXT = 2


    
    Sub Test()
    MsgBox ExcelCount
    End Sub

    Function ExcelCount() As Long
    'http://www.eggheadcafe.com/microsoft/Excel-Programming/31869454/determine-instances-of-excel-open.aspx
    Dim hWin As Long
    Dim nXLinsts As Long
    Dim sBuff As String '* 7
    Const CXL As String = "XLMAIN"
    
    hWin = FindWindow(CXL, vbNullString) ' normally incl app.caption
    
    hWin = GetWindow(hWin, GW_HWNDFIRST)
    
    Do
    hWin = GetWindow(hWin, GW_HWNDNEXT)
    
    Call GetClassName(hWin, sBuff, 7)
    
    If Left$(UCase$(sBuff), 6) = CXL Then
    nXLinsts = nXLinsts + 1
    End If
    
    Loop Until hWin = 0
    
    ExcelCount = nXLinsts
    
    End Function

    Any help is appreciated.

    Thanks,

    BDB
    Last edited by bdb1974; 11-01-2011 at 02:30 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1