Run-time error 1004 Method 'Close' of object '_Workbook' failed

Boomer67

New Member
Joined
Jan 20, 2005
Messages
16
Hi,

I am using Excel 2003 on XP. I have various processes that open workbooks, do some operations on them and then close them again. This normally works fine, but I occasionally get a problem with the code in that it throws a debug error when it gets to the close stage. The simple code is:
Code:
Sub DemonstrateCloseError()
    Workbooks.Open "C:\boomer\Open and Close.xls"
        ' do stuff
    Workbooks("Open and Close.xls").Close savechanges:=True
End Sub
When run the debug error highlights the close line. If I then continue the processing through F8 (step into) or F5 (Run) it continues fine - I do not change anything. This indicates that the code and the file itself are OK, but gives no reason for the halt in processing.

I have even re-installed Excel in an attempt to fix but I still get the problem.

Appreciate if anyone can point me to the cause.

Thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What happens if you use the following:

Code:
Sub TestOpenAndClose()

    Set wbk = Workbooks.Open("C:\boomer\Open and Close.xlsx")
    wbk.Sheets(1).Range("A20") = wbk.Sheets(1).Range("A20") + 1
    wbk.Close SaveChanges:=True

End Sub

Run it 5 times. The next time you open the file, A20 of the first sheet should have 5 on it.

Let us know if this works.

Thanks,

J.
 
Upvote 0
Jonty,

Thanks for the reply.

When I try that it runs until it gets to the close command, then halts again. It won't run through to the end even once without the manual intervention.

Regards
Boomer
 
Upvote 0
In the code between opening and closing, is there anything that changes the designations of the workbooks involved? Mind posting the code here?

Thanks,

J.
 
Upvote 0
Code:
Sub DemonstrateCloseError()
   set wb = Workbooks.Open "C:\boomer\Open and Close.xls"
        ' do stuff
    wb.Close savechanges:=True
End Sub
 
Upvote 0
Jonty,
There are a frw hundred lines of code so maybe not practical to post them all. Basically the process reads data from the file, uses it in the main calling file, and depending on what it the outcome is it may write back to the Open and Close file.

I have tested it again and with save changes set to false and the code runs fine. I am not opening it as read only, so slightly more confused now.
 
Upvote 0
I have changed the code from :
Code:
Sub DemonstrateCloseError()
    Workbooks.Open "C:\boomer\Open and Close.xls"
        ' do stuff
    Workbooks("Open and Close.xls").Close savechanges:=True
End Sub
to
Code:
Sub DemonstrateCloseError()
    Workbooks.Open "C:\boomer\Open and Close.xls"
        ' do stuff
    Workbooks("Open and Close.xls").Save
    Workbooks("Open and Close.xls").Close
End Sub
and this works fine as well. it does seem like a solution but doesn't explain why the original approach does not work.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,108
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top