Checking if a file is open before attempting to open it


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Checking if a file is open before attempting to open it

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    37

    Checking if a file is open before attempting to open it

    Hi,

    I have the following short code to auto open a macro file when teh main fiel is is opened.

    -----------------------------------------------------------------
    Sub Auto_Open()
    '
    ' Auto_Open Macro


    Workbooks.Open Filename:="G:\PUBS\PP-MS\INVOICES\SALEMAC.xlm", Notify:= _
    False
    ActiveWindow.Visible = False
    End Sub

    ---------------------------------------------------

    I want to change this code so that it frst checks if teh file is already open, and only open it if it is not. Can anyone help with it?

    Thanks.

    Talāt

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    If it is already open does it throw an error?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Aug 2009
    Posts
    37
    Yes.

    It first warns that the file is open and re-opening may cause problems. On reply to say do not open displays Microsoft Visual basic error Box

    Runtime error :1004
    Method 'Open' of object -Workbooks' failed

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Then simply trap for the error
    Code:
    Sub Auto_Open()
    On Error GoTo ErrTrap
    '
    ' Auto_Open Macro
    
    
    Workbooks.Open Filename:="G:\PUBS\PP-MS\INVOICES\SALEMAC.xlm", Notify:= _
    False
    ActiveWindow.Visible = False
    Exit Sub
    ErrTrap:
    If Err.Number = 1004 Then
       Msgbox "Workbook already open"
    Else
       'a different error occured - unlikely but possible - so display it
       Msgbox Err.Number & " " & Err.Description
    End If
    End Sub
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Aug 2009
    Posts
    37

    Thumbs up Perfect

    Thanks Hack.

    That's perfect. Work fine!

    Much obliged.

    T.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Error Trapping is something that should be used throughout your application.

    Run time errors that pop your users directly back to the desktop tend to become very, very annoying and can be prevented with error traps.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Checking to see if a paticular file is open or not
    By AM003295 in forum VB Classic
    Replies: 9
    Last Post: 05-19-2009, 02:59 PM
  2. Replies: 1
    Last Post: 01-28-2006, 07:59 AM
  3. Need help checking if file is already saved VB.Net 03
    By boomstick425 in forum VB Classic
    Replies: 1
    Last Post: 01-12-2006, 10:54 AM
  4. open text file using shell
    By Hian Chew in forum VB Classic
    Replies: 18
    Last Post: 03-07-2001, 01:07 PM
  5. Test a file to see if it's open
    By JohnN in forum VB Classic
    Replies: 0
    Last Post: 01-29-2001, 10:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center