-
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
-
If it is already open does it throw an error?
-
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
-
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
-
Perfect
Thanks Hack.
That's perfect. Work fine!
Much obliged.
T.
-
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.
Similar Threads
-
By AM003295 in forum VB Classic
Replies: 9
Last Post: 05-19-2009, 01:59 PM
-
Replies: 1
Last Post: 01-28-2006, 06:59 AM
-
By boomstick425 in forum VB Classic
Replies: 1
Last Post: 01-12-2006, 09:54 AM
-
By Hian Chew in forum VB Classic
Replies: 18
Last Post: 03-07-2001, 12:07 PM
-
By JohnN in forum VB Classic
Replies: 0
Last Post: 01-29-2001, 09: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|