+ Reply to Thread
Results 1 to 5 of 5

Problem opening a file

  1. #1
    Forum Contributor
    Join Date
    03-11-2005
    Posts
    115

    Problem opening a file

    Hi everyone, i have a piece of basic code that calls a workbook

    Workbooks.Open Filename:="\\server1\files\amy\Activity.xls"

    This works perfectly, until I navigate away from Activity.xls back to the main sheet workbook called Main.xls.
    What happens is that when I am back in Main.xls and run the above code to go back to Activity.xls it create a run-time error 1004 as the file is already open.

    Does anyone know if I can add some code to see if the file is open, and it it is activate it, otherwise open the file afresh?

    Thanks for your help.

    Amy xx

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning AmyTaylor

    The code below will test if a named file is open:

    Sub CheckFile()
    On Error Resume Next
    Set a = Workbooks("\\server1\files\amy\Activity.xls")
    If Err = 0 Then
    Windows("\\server1\files\amy\Activity.xls").Activate
    End If
    End Sub

    If the file is already open then it will be made the active sheet. If not, nothing happens.

    HTH

    DominicB

  3. #3
    Forum Contributor
    Join Date
    03-11-2005
    Posts
    115

    Dominic - one more question

    Thanks Dominic, but this isnt exactly what I want. If the file isnt open then I want it to open it, otherwise it just activates the sheet as it will be already open.

    Thanks for your help.
    Amx x

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi Amy

    I know, I know. I seem to have a real problem reading the last few words of a post. Please excuse my silliness and try this instead.

    Sub CheckFile()
    On Error Resume Next
    Set a = Workbooks("\\server1\files\amy\Activity.xls")
    If Err = 0 Then
    Windows("\\server1\files\amy\Activity.xls").Activate
    Exit Sub
    End If
    Workbooks.Open Filename:="\\server1\files\amy\Activity.xls"
    End Sub

    Is that any better?

    HTH

    DominicB

  5. #5
    Forum Contributor
    Join Date
    03-11-2005
    Posts
    115
    Dominic, thank you again ! x

    But.....it always opens the file afresh, and doesnt just activate it if the file is already open - this really isnt a problem but if a user is accessing it across the network on a slow line it could cause a problem.
    Any ideas ?
    Thanks
    Amy xx

+ 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