PC Review


Reply
Thread Tools Rate Thread

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

 
 
Tom Med
Guest
Posts: n/a
 
      24th Sep 2007
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      24th Sep 2007
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


"Tom Med" wrote:

> 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
>
>

 
Reply With Quote
 
Tom Med
Guest
Posts: n/a
 
      24th Sep 2007
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

On 24 Sep, 14:00, Joel <J...@discussions.microsoft.com> wrote:
> 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
>
> "Tom Med" wrote:
> > 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



 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      24th Sep 2007
Try adding in a delay in the Deactivate method. I haven't used Deactivate
method myself in any code I wrote.
"Tom Med" 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
> 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
>
> On 24 Sep, 14:00, Joel <J...@discussions.microsoft.com> wrote:
> > 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
> >
> > "Tom Med" wrote:
> > > 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

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to email workbook from VB6, getting error 1004 Mike S Microsoft Excel Programming 2 14th Aug 2010 11:49 AM
Error 1004 opening workbook 1004 WembleyBear Microsoft Excel Programming 2 30th Nov 2009 01:33 PM
Run time Error 1004, on shared workbook Roxana Microsoft Excel Crashes 2 28th Oct 2008 07:51 PM
commandbar at workbook before close - run time error tango Microsoft Excel Programming 1 24th Oct 2004 10:42 PM
commandbar at workbook before close - run time error tango Microsoft Excel Programming 1 23rd Oct 2004 08:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:00 AM.