Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Cannot close a workbook in VBA due to Run-Time error '1004'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Med - 24 Sep 2007 13:11 GMT
When I try to call this method

  wkbk.Close

I get this error message

 Run-Time error '1004'

 Method 'Close' of object '_Workbook' failed

If I step through the code it will close fine, but the bit of code
that seems to be causing a problem is the
 Sub Workbook_Deactivate()
breaking into this in the debugger seems to resolve the issue, so I
cannot find the cause of the problem.  Is there an error in the way
excel handles the close events?  It seems something is fundamentally
wrong with the way excel is working in this senario.

Thanks
Tom
Joel - 24 Sep 2007 14:00 GMT
This code works.  I can't tell why from one statement what your problem is.  
Maybe you forgot to use SET when you declared wkbk

Sub test()

Workbooks.Open Filename:= _
  ActiveWorkbook.Path & "\" & "tempfile.xls"
 
Set wkbk = ActiveWorkbook

wkbk.Close
End Sub

> When I try to call this method
>
[quoted text clipped - 16 lines]
> Thanks
> Tom
Tom Med - 24 Sep 2007 15:04 GMT
The wkbk is a correct object, I can see it is properly formed in the
local window.  I have a nasty work around which involves trying to
close, if that fails then disabling events and closing again.  I would
still be interested as to why the .close method can fail when you
overload some of the event methods

Tom

> This code works.  I can't tell why from one statement what your problem is.
> Maybe you forgot to use SET when you declared wkbk
[quoted text clipped - 29 lines]
> > Thanks
> > Tom
Joel - 24 Sep 2007 16:10 GMT
Try adding in a delay in the Deactivate method.  I haven't used Deactivate
method myself in any code I wrote.

> The wkbk is a correct object, I can see it is properly formed in the
> local window.  I have a nasty work around which involves trying to
[quoted text clipped - 37 lines]
> > > Thanks
> > > Tom
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.