Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Cannot close a workbook in VBA due to Run-Time error '1004'

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Cannot close a workbook in VBA due to Run-Time error '1004'

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Cannot close a workbook in VBA due to Run-Time error '1004'

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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Cannot close a workbook in VBA due to Run-Time error '1004'

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 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
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
run time error 1004 general odbc error excel 2003 vba Mentos Excel Programming 5 January 24th 11 02:56 PM
run-time error '1004': Application-defined or object-deifined error [email protected] Excel Programming 5 August 10th 05 09:39 PM
commandbar at workbook before close - run time error tango Excel Programming 1 October 24th 04 10:42 PM
commandbar at workbook before close - run time error tango Excel Programming 1 October 23rd 04 08:26 AM
excel workbooks.close returns 1004 error keith Excel Programming 0 September 26th 03 01:05 PM


All times are GMT +1. The time now is 09:41 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"