Consulting

Results 1 to 5 of 5

Thread: Check if workbook is already open

  1. #1
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location

    Check if workbook is already open

    Hi all,

    This should be really basic, but...my memory's failing me

    I have code to open and activate a workbook. The inherent assumption is that the book is closed and it works fine when it is closed, but of course it will error out if the book is already open so there should be an If..Then..Else command there. But how do test if the book is open? (I'm sure I've seen/done this somewhere before...)

    If '<<the workbook is open>> Then
    Workbooks(ArchersName).Activate
    Else
    Application.Workbooks.Open("C:\Windows\Desktop\" & _
                                           "NewKeeper\DBs\" & ArchersName & ".xls") _
                                           .Activate
    End If
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hello,

    I think that this is a John Walkenbach creation, but could be corrected... (Have it stored in a code library here.)

    Private Function WorkbookIsOpen(WBname) As Boolean
    Dim x As Workbook
    On Error Resume Next
    Set x = Workbooks(WBname)
    If Err = 0 Then WorkbookIsOpen = True _
    Else WorkbookIsOpen = False
    End Function
    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Thanx Ken!

    Haven't tried it yet 'cos I did a search and got this one just before your reply arrived (by Joseph Ruben, it works also)

    If Not WorkbookOpen(ArchersName) Then
                Application.Workbooks.Open("C:\Windows\Desktop\" & _
                                      "NewKeeper\DBs\" & ArchersName & ".xls") _
                                      .Activate
          Else
                Workbooks(ArchersName).Activate
          End If
    '//the function for this is:


    Function WorkbookOpen(WorkBookName As String) As Boolean
    ' returns TRUE if the workbook is open
        WorkbookOpen = False
        On Error GoTo WorkBookNotOpen
        If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
            WorkbookOpen = True
            Exit Function
        End If
    WorkBookNotOpen:
    End Function
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    We also have a kb entry on this:

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=207

    But basically just try to activate it to see if you get an error or not. If there is an error then the workbook is not open.

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Thanx Jacob, I KNEW I'd seen this topic before! - but from memery I thort it was in a thread and went searching for it thru pages & pages of threads before posting the question - never thort of the KB (it really shooda been my first thort eh?)
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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