+ Reply to Thread
Results 1 to 8 of 8

check filename

  1. #1
    Bill
    Guest

    check filename

    I have a macro that is opening and closing several workbooks for my Month
    End reports. One of the workbooks treats and places data slightly differently
    than all the other ones, and needs different formatting instructions for that
    data.
    I want to check if that specific filename is open. Is there a specific
    command for that???

    (I could create a flag that is on or off when the file is opened or closed,
    and check that flag for true/false, but I wanted to know if there is a
    command to do this)



  2. #2
    Chip Pearson
    Guest

    Re: check filename

    Try

    Function IsFileOpen(Filename As String) As Boolean
    Dim WB As Workbook
    For Each WB In Workbooks
    If WB.Name = Filename Or WB.FullName = Filename Then
    IsFileOpen = True
    Exit For
    End If
    Next WB
    End Function


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com






    "Bill" <Bill@discussions.microsoft.com> wrote in message
    news:803776BE-B728-48D1-AF74-F1942104278E@microsoft.com...
    >I have a macro that is opening and closing several workbooks for
    >my Month
    > End reports. One of the workbooks treats and places data
    > slightly differently
    > than all the other ones, and needs different formatting
    > instructions for that
    > data.
    > I want to check if that specific filename is open. Is there a
    > specific
    > command for that???
    >
    > (I could create a flag that is on or off when the file is
    > opened or closed,
    > and check that flag for true/false, but I wanted to know if
    > there is a
    > command to do this)
    >
    >




  3. #3
    Ron de Bruin
    Guest

    Re: check filename

    Hi Bill

    You can use this function in a normal module

    Function bIsBookOpen(ByRef szBookName As String) As Boolean
    ' Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
    End Function

    And use it like this in your code
    If bIsBookOpen("test.xls") Then


    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "Bill" <Bill@discussions.microsoft.com> wrote in message news:803776BE-B728-48D1-AF74-F1942104278E@microsoft.com...
    >I have a macro that is opening and closing several workbooks for my Month
    > End reports. One of the workbooks treats and places data slightly differently
    > than all the other ones, and needs different formatting instructions for that
    > data.
    > I want to check if that specific filename is open. Is there a specific
    > command for that???
    >
    > (I could create a flag that is on or off when the file is opened or closed,
    > and check that flag for true/false, but I wanted to know if there is a
    > command to do this)
    >
    >




  4. #4
    Jim Thomlinson
    Guest

    RE: check filename

    Here is a simple function you can use. It is adapted from some code that I
    got from Chip Pearson.

    Public Function BookOpen(SName As String) As Boolean
    On Error Resume Next
    BookOpen = CBool(Len(Workbooks(SName).Name))
    End Function

    You would use it like this...

    msgbox worbookopen("MyBook.xls")
    --
    HTH...

    Jim Thomlinson


    "Bill" wrote:

    > I have a macro that is opening and closing several workbooks for my Month
    > End reports. One of the workbooks treats and places data slightly differently
    > than all the other ones, and needs different formatting instructions for that
    > data.
    > I want to check if that specific filename is open. Is there a specific
    > command for that???
    >
    > (I could create a flag that is on or off when the file is opened or closed,
    > and check that flag for true/false, but I wanted to know if there is a
    > command to do this)
    >
    >


  5. #5
    Jim Thomlinson
    Guest

    RE: check filename

    Sorry you would use it like...

    msgbox bookopen("MyBook.xls")
    --
    HTH...

    Jim Thomlinson


    "Jim Thomlinson" wrote:

    > Here is a simple function you can use. It is adapted from some code that I
    > got from Chip Pearson.
    >
    > Public Function BookOpen(SName As String) As Boolean
    > On Error Resume Next
    > BookOpen = CBool(Len(Workbooks(SName).Name))
    > End Function
    >
    > You would use it like this...
    >
    > msgbox worbookopen("MyBook.xls")
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "Bill" wrote:
    >
    > > I have a macro that is opening and closing several workbooks for my Month
    > > End reports. One of the workbooks treats and places data slightly differently
    > > than all the other ones, and needs different formatting instructions for that
    > > data.
    > > I want to check if that specific filename is open. Is there a specific
    > > command for that???
    > >
    > > (I could create a flag that is on or off when the file is opened or closed,
    > > and check that flag for true/false, but I wanted to know if there is a
    > > command to do this)
    > >
    > >


  6. #6
    AA2e72E
    Guest

    RE: check filename

    The suggested approach/solution(s) will work only if the workbook is open in
    the CURRENT Excel session; they will fail if the file is open in another
    session of Excel, such as an automation session.

    This will return TRUE is the fil is open in ANY excel session.

    Function WBOpen(ByVal Filename As String) As Boolean
    On Error Resume Next
    Set wb = GetObject(Filename)
    If Not IsEmpty(wb) Then
    WBOpen = True
    Set wb = Nothing
    End If
    End Function




  7. #7
    Bill
    Guest

    RE: check filename

    Thank you all. It now works.

    "Bill" wrote:

    > I have a macro that is opening and closing several workbooks for my Month
    > End reports. One of the workbooks treats and places data slightly differently
    > than all the other ones, and needs different formatting instructions for that
    > data.
    > I want to check if that specific filename is open. Is there a specific
    > command for that???
    >
    > (I could create a flag that is on or off when the file is opened or closed,
    > and check that flag for true/false, but I wanted to know if there is a
    > command to do this)
    >
    >


  8. #8
    GS
    Guest

    RE: check filename

    <FWIW>

    While the suggested approache(s)/solution(s) do address the OP's situation
    explicitly (ie. his current session), your suggestion doesn't work because
    it returns TRUE if the file exists, whether it's open or not, or in use or
    not.

    You might also want to specify that this test requires that wb be Variant
    type. Even though it appears that it is (implicit in your code), someone
    reading this may spent a lot of frustrating time trying to figure that out
    using Option Explicit.


    Regards,
    Garry

+ Reply to Thread

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