Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Before_Close problem

Hi

Before a workbook is closed I want to give user the option
of opening another one.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
if Msgbox("Do you need to open 'Data2.xls'", vbYesNo) =
vbYes then
workbooks.open "C:\Data\Date2.xls"
End if
End Sub

The problem is that while the second workbook Data2 is
opened ok, the original workbook which this code is in
does not close.
I don't understand this as it's in a BeforeClose sub
I've tried putting Else ThisWorkbook.close in, but this
causes the message box to be displayed all over again.

Please Help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Before_Close problem

Roberto,

I can confirm the behavior you describe. It looks like a bug to me. I can't
think of a good work around. Sorry.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Roberto" wrote in message
...
Hi

Before a workbook is closed I want to give user the option
of opening another one.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
if Msgbox("Do you need to open 'Data2.xls'", vbYesNo) =
vbYes then
workbooks.open "C:\Data\Date2.xls"
End if
End Sub

The problem is that while the second workbook Data2 is
opened ok, the original workbook which this code is in
does not close.
I don't understand this as it's in a BeforeClose sub
I've tried putting Else ThisWorkbook.close in, but this
causes the message box to be displayed all over again.

Please Help



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Before_Close problem

It seemed to run ok for me in xl2002 (SP2).

In fact, if the original workbook were dirty, then I'd even get prompted to
save.

So I'm guessing you're not using xl2002.

Maybe you can use application.ontime to call a macro that will close the
workbook.

This didn't work in xl2002--since the workbook was already closed and excel
reopened it to run the closeme sub:

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Do you need to open 'Data2.xls'", vbYesNo) = vbYes Then
Application.OnTime Now + TimeSerial(0, 0, 5), "CloseMe"
Workbooks.Open "C:\my documents\excel\book1.xls"
End If
End Sub

And in a general module:

Option Explicit
Sub closeme()
ThisWorkbook.Close savechanges:=True
End Sub


Roberto wrote:

Hi

Before a workbook is closed I want to give user the option
of opening another one.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
if Msgbox("Do you need to open 'Data2.xls'", vbYesNo) =
vbYes then
workbooks.open "C:\Data\Date2.xls"
End if
End Sub

The problem is that while the second workbook Data2 is
opened ok, the original workbook which this code is in
does not close.
I don't understand this as it's in a BeforeClose sub
I've tried putting Else ThisWorkbook.close in, but this
causes the message box to be displayed all over again.

Please Help


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Before_Close problem

Hi

No, I'm using Excel 97
Even if I don't have the message box and just put the
Workbook.Open bit, the original workbook remains open.

Maybe it is a bug then, if it works for you?

-----Original Message-----
It seemed to run ok for me in xl2002 (SP2).

In fact, if the original workbook were dirty, then I'd

even get prompted to
save.

So I'm guessing you're not using xl2002.

Maybe you can use application.ontime to call a macro that

will close the
workbook.

This didn't work in xl2002--since the workbook was

already closed and excel
reopened it to run the closeme sub:

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Do you need to open 'Data2.xls'", vbYesNo) =

vbYes Then
Application.OnTime Now + TimeSerial(0, 0,

5), "CloseMe"
Workbooks.Open "C:\my documents\excel\book1.xls"
End If
End Sub

And in a general module:

Option Explicit
Sub closeme()
ThisWorkbook.Close savechanges:=True
End Sub


Roberto wrote:

Hi

Before a workbook is closed I want to give user the

option
of opening another one.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
if Msgbox("Do you need to open 'Data2.xls'", vbYesNo) =
vbYes then
workbooks.open "C:\Data\Date2.xls"
End if
End Sub

The problem is that while the second workbook Data2 is
opened ok, the original workbook which this code is in
does not close.
I don't understand this as it's in a BeforeClose sub
I've tried putting Else ThisWorkbook.close in, but this
causes the message box to be displayed all over again.

Please Help


--

Dave Peterson

.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Before_Close problem

Roberto,

You code works fine for me in XL97(SP1), ie Date2.xls
opens and/or is activated then the code file closes.

Perhaps not a version issue but something else in common
between your & Chip's setups but not Dave's & mine, but I
can't imagine what.

A slight variation on Dave's code with Cancel and a flag:

Option Explicit
Private CloseFlag As Boolean

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not CloseFlag Then
If MsgBox("Do you need to open 'Data2.xls'", _
vbYesNo) = vbYes Then
Cancel = True
Workbooks.Open "C:\Temp\Data2.xls"
'Application.OnTime Now + TimeSerial(0, 0,
5), "CloseMe"
Application.OnTime Now, "CloseMe"
CloseFlag = True
End If
'Else
'MsgBox "Now closing"
End If
End Sub

And in a normal module

Option Explicit
Sub CloseMe()
ThisWorkbook.Close savechanges:=True
End Sub

I didn't need to delay the OnTime macro, but then I didn't
have a 'problem' so you might.

Regards,
Sandy
savituk yahoo co uk

-----Original Message-----
Hi

No, I'm using Excel 97
Even if I don't have the message box and just put the
Workbook.Open bit, the original workbook remains open.

Maybe it is a bug then, if it works for you?

-----Original Message-----
It seemed to run ok for me in xl2002 (SP2).

In fact, if the original workbook were dirty, then I'd

even get prompted to
save.

So I'm guessing you're not using xl2002.

Maybe you can use application.ontime to call a macro

that
will close the
workbook.

This didn't work in xl2002--since the workbook was

already closed and excel
reopened it to run the closeme sub:

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Do you need to open 'Data2.xls'", vbYesNo) =

vbYes Then
Application.OnTime Now + TimeSerial(0, 0,

5), "CloseMe"
Workbooks.Open "C:\my documents\excel\book1.xls"
End If
End Sub

And in a general module:

Option Explicit
Sub closeme()
ThisWorkbook.Close savechanges:=True
End Sub


Roberto wrote:

Hi

Before a workbook is closed I want to give user the

option
of opening another one.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
if Msgbox("Do you need to open 'Data2.xls'", vbYesNo) =
vbYes then
workbooks.open "C:\Data\Date2.xls"
End if
End Sub

The problem is that while the second workbook Data2 is
opened ok, the original workbook which this code is in
does not close.
I don't understand this as it's in a BeforeClose sub
I've tried putting Else ThisWorkbook.close in, but this
causes the message box to be displayed all over again.

Please Help


--

Dave Peterson

.

.



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
Colon at the end of excel file name(ex: problem.xls:1, problem.xls financeguy New Users to Excel 2 January 15th 10 01:15 AM
Wierd one concerning Before_Close and OnTime Antonio Excel Discussion (Misc queries) 14 June 9th 06 05:33 PM
Started out as an Access problem. Now an Excel problem RobertM Excel Discussion (Misc queries) 2 April 26th 06 07:30 PM
problem with a conditional max problem Brian Cornejo Excel Discussion (Misc queries) 1 February 18th 05 06:25 PM
Auto_Open & Before_Close John Wilson Excel Programming 0 August 6th 03 09:04 PM


All times are GMT +1. The time now is 10:51 AM.

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

About Us

"It's about Microsoft Excel"