Consulting

Results 1 to 13 of 13

Thread: How to define workbook is open in which process

  1. #1
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location

    How to define workbook is open in which process

    I have open two Excel process (Ctrl + Alt + Delete, and will see 2 process)
    I can not use the following code to check if I open one workbook in other process.
    Function bWorkbookIsOpen(rsWbkName As String) As Boolean 
    On Error Resume Next   
    bWorkbookIsOpen = CBool(Len(Workbooks(rsWbkName).Name) > 0) 
    End Function 
    
    Le Van Duyet

  2. #2
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Hi Le Van Duyet,
    The code below helps to determine if the file is open with exclusive access in other process or by other users:
    
    Function IsFileOpen(FilePathName As String) As Boolean
      Dim FN%
      FN = FreeFile
      On Error Resume Next
      Open FilePathName For Random Access Read Write Lock Read Write As #FN
      Close #FN
      IsFileOpen = Err <> 0
    End Function
    
    Sub Test()
      Dim MyFile As String
      MyFile = "C:\Test.xls"
      MsgBox "IsFileOpen = " & IsFileOpen(MyFile), , MyFile
    End Sub
    See also: Check If a File Is Already Open

    Regards,
    Vladimir
    Last edited by ZVI; 04-21-2010 at 08:10 PM.

  3. #3
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Hi ZVI,

    Thank for your fast reply.
    In my case that I just want to check one workbook, it means that I pass the workbook name to this function only (not the full path).

    Le Van Duyet

  4. #4
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by levanduyet
    ...In my case that I just want to check one workbook, it means that I pass the workbook name to this function only (not the full path)
    To check the presence of workbook name in window text of each Excel instance the API solution can help:
    
    ' ZVI:2010-04-23 - API solution to find workbook name in text of any Excel window
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Dim hwnd As Long, IsOpen As Boolean, WbName As String
    
    ' Testing subrotine
    Sub Test()
      Const Wb = "Test.xls"
      MsgBox "IsWorkbookOpen = " & IsWorkbookOpen(Wb) & vbLf & WbName, , Wb
    End Sub
    
    ' IsFileOpen = True if WorkbookName is found in text of any Excel window
    Function IsWorkbookOpen(WorkbookName As String) As Boolean
      hwnd = 0: IsOpen = False: WbName = UCase(WorkbookName)
      EnumChildWindows hwnd, AddressOf EnumChildProc, ByVal 0&
      If IsOpen Then IsWorkbookOpen = True Else WbName = ""
    End Function
    
    ' Aux function for API callback
    Private Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim s$
        s = Space$(GetWindowTextLength(hwnd) + 1)
        GetWindowText hwnd, s, Len(s)
        s = Left$(s, Len(s) - 1)
        If UCase(s) Like "MICROSOFT EXCEL *" & WbName & "*" Then
          WbName = s
          IsOpen = True
          Exit Function
        End If
        EnumChildProc = 1
    End Function
    Replace "MICROSOFT EXCEL *" by "*" to include XLA Add-Ins project names in the search.

    Vladimir
    Last edited by ZVI; 04-22-2010 at 05:11 PM.

  5. #5
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Thank ZIV,

    I have tested. It does not work.

    Le Van Duyet

  6. #6
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by levanduyet
    Thank ZIV,

    I have tested. It does not work.

    Le Van Duyet
    Could you please describe your testing steps?
    This can be localization issue, try to replace "MICROSOFT EXCEL *" by "*" and test it again.
    Exclude the file extention from the passed workbook name in case of workbook is created but not saved.
    Last edited by ZVI; 04-22-2010 at 05:08 PM.

  7. #7
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Hi ZVI,

    If you test the workbook in the same session, it's not correct.

    Le Van Duyet

  8. #8
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Dear All,
    Anyone have another solution?
    If the user that they change the window caption text, or there are some workbooks in one process?

    Tks,

    LVD

  9. #9
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by levanduyet
    Hi ZVI,

    If you test the workbook in the same session, it's not correct.

    Le Van Duyet
    Dear Le Van Duyet,

    The proposed in post #4 code works with windows in the same Excel session as well as in other sessions.
    It’s still not clear for me why you say it doesn’t.
    Just add new workbook to Excel session and try to check it out.
    Could you post your testing code you have stuck in?

    Nothing is said in your previous posts about changing of the window caption.
    And yes, it is possible by API code to find Excel windows even if its caption is modified.
    But without clearing of your initial (previous) task it has not sense for me.

    Vladimir
    Last edited by ZVI; 04-26-2010 at 12:00 PM.

  10. #10
    levanduet.
    Try this.
    [vba]Private Sub Workbook_Open()
    Dim Wb As Workbook
    For Each Wb In Application.Workbooks
    If Not Wb.Name = ThisWorkbook.Name Then
    MsgBox Wb.Name
    Wb.Close
    End If
    Next Wb[/vba]

    ZVI.
    You code #4 dont work for me to??
    Just says:IsWorkbookOpen =false

  11. #11
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Hi Omnibuster,
    This code, I know. But it's can not check in difference process.
    (Ctrl + Alt + Delete)

    Le Van Duyet

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @ZVI:

    Greetings Vladimir,

    I am afraid this message may seem 'choppy', but technical difficulties (site went off once, my POL locked, server at work went down...ERGH!!!) have meant that this is either my third or fourth try... (and I ain't a fast typist)

    Onward and Upward:

    I tested in xl2000 and 2003, both WIN/XP.

    Initial criteria:

    I added, [vba]If UCase(s) Like "MICROSOFT*" Then Debug.Print s[/vba]
    right below [vba]s = Left$(s, Len(s) - 1)[/vba]

    I also rem'd (commented out) the Exit Function.

    After getting less than stellar results, I substituted [vba]Debug.Print s[/vba]

    Tests were with:

    One instance of Excel
    Two workbooks open, the wb with the code (hereafter: codewb) and the wb to 'find.'

    First tests with workbook windows maximized:

    With codewb active, msgbox reports False.

    I noted the following returns for 's' that seemed of interest:

    Microsoft Excel - vbax31648#4_Vladimir_IsWorkbookOpen.xls (<---codewb)

    Test Referencing Another Sheet.xls

    Microsoft Visual Basic - Test Referencing Another Sheet.xls [running] - [Module1 (Code)] (<---even though VBIDE was closed)

    vbax31648#4_Vladimir_IsWorkbookOpen.xls

    With 'lookfor' wb being active, SUCCESS, msgbox returned True, returns for 's'...
    Microsoft Excel - Test Referencing Another Sheet.xls

    vbax31648#4_Vladimir_IsWorkbookOpen.xls

    Microsoft Visual Basic - Test Referencing Another Sheet.xls [running] - [Module1 (Code)]

    Test Referencing Another Sheet.xls


    With the wb windows sized normal:

    With codewb active, msgbox reports False.

    Returns for 's' of interest:

    Microsoft Excel

    Test Referencing Another Sheet.xls

    Microsoft Visual Basic - Test Referencing Another Sheet.xls [running] - [Module1 (Code)]

    vbax31648#4_Vladimir_IsWorkbookOpen.xls


    With 'lookfor' wb being active, FAILURE, msgbox reports False, 's' returns:

    Microsoft Excel

    vbax31648#4_Vladimir_IsWorkbookOpen.xls

    Microsoft Visual Basic - vbax31648#4_Vladimir_IsWorkbookOpen.xls [running] - [Module1 (Code)]

    Test Referencing Another Sheet.xls



    Hopefully the testing is of some help. I admit that the only thing I know about API is what little I have been able to glean from posts/examples such as yours here.

    That said, it would seem to me that the wb.Name is being found in a caption, but that maybe we should change the Like test a bit. Would...
    [vba] If UCase(s) Like "*" & WbName Then[/vba]
    be worthy of consideration? I only briefly tested (I am afraid I should have been to bed several hours ago, but this is interesting), but it seems to work in the same instance, in another instance, both regardless of window status.

    Mark

  13. #13
    Thanks GTO.
    I was testing different ways: msgbos(s), UCase(s) e.g say WbkName right, but function says =false(notopened).
    [vba]
    If Application.Caption = "Microsoft Excel – Wbkname” Then
    [/vba]
    AppCaption Works only if Wbk is visible and activated.
    But need control when Application.visible=0

Posting Permissions

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