Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default runtime error 1004

hi...

am opening the file through the GetOpenFilename method. I am facing an
error in of the cases... Like say or example i try and open a file with the
XYZ.xls...which is already open. the system generates a mess saying
"reopening will cause any changes you made to be discarded. do you want to
reopen XYZ.xls?"

if i click yes...it works fine by reopening the file but when i click to
No...Runtime error 1004 comes:

"Method open of object workbooks failed "

and its giving an error here in the code: Workbooks.Open Filename:=sFilename

how do i do the error handling in this case.

thanks a lot
Monika


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default runtime error 1004

Hi,

You can trap this error and look for it in an error handler.
Please see this example:

Sub OpenWorkBook()

Dim varOpenFile As Variant
Dim objWorkbook As Workbook

On Error GoTo OpenWorkBook_Err

' Display open dialog.
varOpenFile = Application.GetOpenFilename(MultiSelect:=False)

' If user cancels the open workbook dialog exit sub.
If varOpenFile = False Then GoTo OpenWorkBook_Exit

' Open workbook.
' If err number 1004 occurs here it will be trapped in the error
handler.
Set objWorkbook = Workbooks.Open(varOpenFile)

' Etc....

OpenWorkBook_Exit:

Exit Sub

GetOpenWorkBooks_Err:

Select Case Err.Number

Case 1004 ' If user answers no to the dialog to open the file read-only
just exit sub or display a warning to the user.
MsgBox "The workbook you want to open is already opened. Please try
again.", vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

Case Else
' In case of any other error.
MsgBox Err.Number & " " & Err.Source & " " & Err.Description ,
vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

End Select

End Sub


HTH

/Ulrik

"monika" skrev i meddelandet
...
hi...

am opening the file through the GetOpenFilename method. I am facing an
error in of the cases... Like say or example i try and open a file with

the
XYZ.xls...which is already open. the system generates a mess saying
"reopening will cause any changes you made to be discarded. do you want to
reopen XYZ.xls?"

if i click yes...it works fine by reopening the file but when i click to
No...Runtime error 1004 comes:

"Method open of object workbooks failed "

and its giving an error here in the code: Workbooks.Open

Filename:=sFilename

how do i do the error handling in this case.

thanks a lot
Monika




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default runtime error 1004

A small typo went into in the code - please use this corrected version
instead:

Sub OpenWorkBook()

Dim varOpenFile As Variant
Dim objWorkbook As Workbook

On Error GoTo OpenWorkBook_Err

' Display open dialog.
varOpenFile = Application.GetOpenFilename(MultiSelect:=False)

' If user cancels the open workbook dialog exit sub.
If varOpenFile = False Then GoTo OpenWorkBook_Exit

' Open workbook.
' If err number 1004 occurs here it will be trapped in the error
handler.
Set objWorkbook = Workbooks.Open(varOpenFile)

' Etc....

OpenWorkBook_Exit:

Exit Sub

OpenWorkBooks_Err:

Select Case Err.Number

Case 1004 ' If user answers no to the dialog to open the file read-only
just exit sub or display a warning to the user.
MsgBox "The workbook you want to open is already opened. Please try
again.", vbCritical, Application.Name
Resume OpenWorkBook_Exit

Case Else
' In case of any other error.
MsgBox Err.Number & " " & Err.Source & " " & Err.Description ,
vbCritical, Application.Name
Resume OpenWorkBook_Exit

End Select

End Sub

HTH

/Ulrik




"Ulrik Gustafsson" wrote in message
...
Hi,

You can trap this error and look for it in an error handler.
Please see this example:

Sub OpenWorkBook()

Dim varOpenFile As Variant
Dim objWorkbook As Workbook

On Error GoTo OpenWorkBook_Err

' Display open dialog.
varOpenFile = Application.GetOpenFilename(MultiSelect:=False)

' If user cancels the open workbook dialog exit sub.
If varOpenFile = False Then GoTo OpenWorkBook_Exit

' Open workbook.
' If err number 1004 occurs here it will be trapped in the error
handler.
Set objWorkbook = Workbooks.Open(varOpenFile)

' Etc....

OpenWorkBook_Exit:

Exit Sub

GetOpenWorkBooks_Err:

Select Case Err.Number

Case 1004 ' If user answers no to the dialog to open the file

read-only
just exit sub or display a warning to the user.
MsgBox "The workbook you want to open is already opened. Please

try
again.", vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

Case Else
' In case of any other error.
MsgBox Err.Number & " " & Err.Source & " " & Err.Description ,
vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

End Select

End Sub


HTH

/Ulrik

"monika" skrev i meddelandet
...
hi...

am opening the file through the GetOpenFilename method. I am facing an
error in of the cases... Like say or example i try and open a file with

the
XYZ.xls...which is already open. the system generates a mess saying
"reopening will cause any changes you made to be discarded. do you want

to
reopen XYZ.xls?"

if i click yes...it works fine by reopening the file but when i click to
No...Runtime error 1004 comes:

"Method open of object workbooks failed "

and its giving an error here in the code: Workbooks.Open

Filename:=sFilename

how do i do the error handling in this case.

thanks a lot
Monika






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default runtime error 1004

thanks tremendously... this is exactly what i was looking for...and i tried
it works fine... infact this gave me a little idea as to how to manage the
error handling.

reg
Monika
"Ulrik Gustafsson" wrote in message
...
A small typo went into in the code - please use this corrected version
instead:

Sub OpenWorkBook()

Dim varOpenFile As Variant
Dim objWorkbook As Workbook

On Error GoTo OpenWorkBook_Err

' Display open dialog.
varOpenFile = Application.GetOpenFilename(MultiSelect:=False)

' If user cancels the open workbook dialog exit sub.
If varOpenFile = False Then GoTo OpenWorkBook_Exit

' Open workbook.
' If err number 1004 occurs here it will be trapped in the error
handler.
Set objWorkbook = Workbooks.Open(varOpenFile)

' Etc....

OpenWorkBook_Exit:

Exit Sub

OpenWorkBooks_Err:

Select Case Err.Number

Case 1004 ' If user answers no to the dialog to open the file

read-only
just exit sub or display a warning to the user.
MsgBox "The workbook you want to open is already opened. Please

try
again.", vbCritical, Application.Name
Resume OpenWorkBook_Exit

Case Else
' In case of any other error.
MsgBox Err.Number & " " & Err.Source & " " & Err.Description ,
vbCritical, Application.Name
Resume OpenWorkBook_Exit

End Select

End Sub

HTH

/Ulrik




"Ulrik Gustafsson" wrote in message
...
Hi,

You can trap this error and look for it in an error handler.
Please see this example:

Sub OpenWorkBook()

Dim varOpenFile As Variant
Dim objWorkbook As Workbook

On Error GoTo OpenWorkBook_Err

' Display open dialog.
varOpenFile = Application.GetOpenFilename(MultiSelect:=False)

' If user cancels the open workbook dialog exit sub.
If varOpenFile = False Then GoTo OpenWorkBook_Exit

' Open workbook.
' If err number 1004 occurs here it will be trapped in the error
handler.
Set objWorkbook = Workbooks.Open(varOpenFile)

' Etc....

OpenWorkBook_Exit:

Exit Sub

GetOpenWorkBooks_Err:

Select Case Err.Number

Case 1004 ' If user answers no to the dialog to open the file

read-only
just exit sub or display a warning to the user.
MsgBox "The workbook you want to open is already opened. Please

try
again.", vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

Case Else
' In case of any other error.
MsgBox Err.Number & " " & Err.Source & " " & Err.Description ,
vbCritical, Application.Name
Resume GetOpenWorkBook_Exit

End Select

End Sub


HTH

/Ulrik

"monika" skrev i meddelandet
...
hi...

am opening the file through the GetOpenFilename method. I am facing

an
error in of the cases... Like say or example i try and open a file

with
the
XYZ.xls...which is already open. the system generates a mess saying
"reopening will cause any changes you made to be discarded. do you

want
to
reopen XYZ.xls?"

if i click yes...it works fine by reopening the file but when i click

to
No...Runtime error 1004 comes:

"Method open of object workbooks failed "

and its giving an error here in the code: Workbooks.Open

Filename:=sFilename

how do i do the error handling in this case.

thanks a lot
Monika








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error 1004 JB Bates[_2_] Excel Discussion (Misc queries) 5 December 29th 09 02:40 PM
runtime error '1004' Steve Excel Discussion (Misc queries) 1 April 28th 06 08:58 PM
What causes runtime error 1004? [email protected] Excel Discussion (Misc queries) 4 October 27th 05 07:15 PM
Runtime Error '1004' [email protected] Excel Discussion (Misc queries) 2 July 18th 05 06:10 AM
Runtime error 1004 Gary[_4_] Excel Programming 1 October 10th 03 02:21 PM


All times are GMT +1. The time now is 01:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"