Results 1 to 5 of 5

check if a file is already open in VBA,

This is a discussion on check if a file is already open in VBA, within the Excel Questions forums, part of the Question Forums category; How can i check if a file is already open in VBA , i get a run time error because ...

  1. #1
    New Member
    Join Date
    May 2004
    Posts
    26

    Default check if a file is already open in VBA,

    How can i check if a file is already open in VBA, i get a run time error because the file is already open and it cant open the file of same name although it is in a different directory , so i just want to have a function that return a boolean if already open so that i can just activate the file instead trying to open from the second location
    Its 100 am i still need to sleep from yesterday so any urgent help would be very much apprectaited

  2. #2
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    11,001

    Default Re: check if a file is already open in VBA,

    Many ways to do this, here's one possibility, modify for path and file name:

    Sub Test2()
    On Error Resume Next
    Workbooks("YourFileName").Activate
    If Err <> 0 Then Err.Clear: Workbooks.Open ("C:\Your\File\Path\YourFileName.xls")
    End Sub

  3. #3
    New Member
    Join Date
    May 2004
    Posts
    26

    Default Re: check if a file is already open in VBA,

    i dont understand how this works or is going to work
    if it has an error it ll just move to next.
    why do i activate a workbook first then if error try opening it?
    could you put some quick short comments to explain the code so that i can understand if it is what i need
    thanks

  4. #4
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    11,001

    Default Re: check if a file is already open in VBA,

    Instead of sitting there with this "urgent" matter wondering out loud about the code while your clock is ticking, why don't you just test it to see for yourself if it's what you need? It works fine based on what you said you want, which is, it activates your workbook if it is open, and if it is not open it opens it from the file path.

  5. #5
    New Member
    Join Date
    May 2004
    Posts
    26

    Default Re: check if a file is already open in VBA,

    would this work on a similar bases to what u supplied ealier .....caus i now have a situation where the file has been closed but i'm trying to save it. I need to accomadate for this situation that if the file is closed then i need to proceed, in my main func i have on error goto a and an a: label, but it doesnt seem to work, any other ways
    thanks for before it did work.


    Sub SaveIfFileOpen(Filename As String)
    On Error Resume Next
    Workbooks(Filename).Activate
    ActiveWorkbook.SaveAs Filename:= _
    "D:\Projects\myproj\amy\extFiles\mrpst\dupIDFail\" & ActiveWorkbook.Name _
    , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False

    If Err <> 0 Then Err.Clear: Exit Sub
    End Sub

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com