error opening a workbook

B

budi1568

of ObjectHallo
Is there anybody can help me since I am new in VB and having case as follow:
I am working with two workbook where when I open the 1st workbook it will
open the second one. The problem is if the second file already open I got an
error message : Run Time Error 1004.
Methode 'OPen of Object 'Workbooks' failed

Private Sub Workbook_Open()
Workbooks.Open Filename:="O:\2009 Budget\Forecast 2008\Forecast Q Rep
08.xls" _
, UpdateLinks:=0
End Sub

Thanks for helping me
 
D

Dave Peterson

Check to see if it's open first:

Private Sub Workbook_Open()
dim TestWkbk as workbook
set testwkbk = nothing
on error resume next
set testwkbk = workbooks("forecast q rep 08.xls") '<- no path here
on error resume next

if testwkbk is nothing then
'it's not open
set testwkbk = Workbooks.Open _
(Filename:="O:\2009 Budget\Forecast 2008\Forecast Q Rep 08.xls", _
UpdateLinks:=0)
end if

End Sub

(Untested, uncompiled--watch for typos)
 
B

budi1568

Great!!!
Thanks Dave...really works fine


Dave Peterson said:
Check to see if it's open first:

Private Sub Workbook_Open()
dim TestWkbk as workbook
set testwkbk = nothing
on error resume next
set testwkbk = workbooks("forecast q rep 08.xls") '<- no path here
on error resume next

if testwkbk is nothing then
'it's not open
set testwkbk = Workbooks.Open _
(Filename:="O:\2009 Budget\Forecast 2008\Forecast Q Rep 08.xls", _
UpdateLinks:=0)
end if

End Sub

(Untested, uncompiled--watch for typos)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top