Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why doesn't Workbook.Save work?

I'm working on an Excel add-in and found some unexpected behavior that I hope
someone might be able to explain to me.

The add-in handles the WorkbookBeforeClose event, and possibly saves the
Workbook. This works fine if the close is done manually -- that is, by
clicking the 'X' button. But if the close is done programmatically through a
call to Workbook.Close, then saving doesn't work.

I've written some VBA that replicates the problem (though to be clear, the
add-in is more complicated, doing more than just a simple save).

First is a class called XLEvents.

Code:

Private WithEvents xlApp As Excel.Application

Private Sub Class_Initialize()
Set xlApp = Application
End Sub

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
MsgBox "Closing"
Wb.Save
End Sub

I also have a macro that simply closes the Workbook. This isn't part of my
add-in's behavior -- it just illustrates the issue, and is something that
could happen if the user has their own macro, another add-in, etc.

Code:

Sub CloseWorkbook()
Application.ActiveWorkbook.Close
End Sub

This is the behavior I see:

Manual

1. Open the workbook.
2. Make a small edit.
3. Close the workbook by clicking the 'X'.
4. The "Closing" message box appears; click OK.
5. The workbook closes.

Programmatic

1. Open the workbook.
2. Make a small edit.
3. Run the "CloseWorkbook" macro.
4. The "Closing" message box appears; click OK.
5. Excel asks if I want to save.

#5 in the programmatic case is a symptom that the save didn't do anything in
this case. I can also verify that nothing got written to disk. Why would this
be? Is there a way to cause the programmatic behavior to match the manual
behavior (such that Save works)?

Thanks,
Eric
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Why doesn't Workbook.Save work?

ActiveWorkbook.Save
Workbooks(1).Save
Workbooks("Book1").Save

"Eric Smith" wrote:

I'm working on an Excel add-in and found some unexpected behavior that I hope
someone might be able to explain to me.

The add-in handles the WorkbookBeforeClose event, and possibly saves the
Workbook. This works fine if the close is done manually -- that is, by
clicking the 'X' button. But if the close is done programmatically through a
call to Workbook.Close, then saving doesn't work.

I've written some VBA that replicates the problem (though to be clear, the
add-in is more complicated, doing more than just a simple save).

First is a class called XLEvents.

Code:

Private WithEvents xlApp As Excel.Application

Private Sub Class_Initialize()
Set xlApp = Application
End Sub

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
MsgBox "Closing"
Wb.Save
End Sub

I also have a macro that simply closes the Workbook. This isn't part of my
add-in's behavior -- it just illustrates the issue, and is something that
could happen if the user has their own macro, another add-in, etc.

Code:

Sub CloseWorkbook()
Application.ActiveWorkbook.Close
End Sub

This is the behavior I see:

Manual

1. Open the workbook.
2. Make a small edit.
3. Close the workbook by clicking the 'X'.
4. The "Closing" message box appears; click OK.
5. The workbook closes.

Programmatic

1. Open the workbook.
2. Make a small edit.
3. Run the "CloseWorkbook" macro.
4. The "Closing" message box appears; click OK.
5. Excel asks if I want to save.

#5 in the programmatic case is a symptom that the save didn't do anything in
this case. I can also verify that nothing got written to disk. Why would this
be? Is there a way to cause the programmatic behavior to match the manual
behavior (such that Save works)?

Thanks,
Eric

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Why doesn't Workbook.Save work?

There's probably a more robust way to do this, but this should provide a
starting point. The key is to set Cancel = True.

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)

Cancel = True
MsgBox "Saving"
Wb.Save
xlApp.EnableEvents = False
Wb.Close False
xlApp.EnableEvents = True

End Sub

"Eric Smith" wrote in message
...
I'm working on an Excel add-in and found some unexpected behavior that I
hope
someone might be able to explain to me.

The add-in handles the WorkbookBeforeClose event, and possibly saves the
Workbook. This works fine if the close is done manually -- that is, by
clicking the 'X' button. But if the close is done programmatically through
a
call to Workbook.Close, then saving doesn't work.

I've written some VBA that replicates the problem (though to be clear, the
add-in is more complicated, doing more than just a simple save).

First is a class called XLEvents.

Code:

Private WithEvents xlApp As Excel.Application

Private Sub Class_Initialize()
Set xlApp = Application
End Sub

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
MsgBox "Closing"
Wb.Save
End Sub

I also have a macro that simply closes the Workbook. This isn't part of my
add-in's behavior -- it just illustrates the issue, and is something that
could happen if the user has their own macro, another add-in, etc.

Code:

Sub CloseWorkbook()
Application.ActiveWorkbook.Close
End Sub

This is the behavior I see:

Manual

1. Open the workbook.
2. Make a small edit.
3. Close the workbook by clicking the 'X'.
4. The "Closing" message box appears; click OK.
5. The workbook closes.

Programmatic

1. Open the workbook.
2. Make a small edit.
3. Run the "CloseWorkbook" macro.
4. The "Closing" message box appears; click OK.
5. Excel asks if I want to save.

#5 in the programmatic case is a symptom that the save didn't do anything
in
this case. I can also verify that nothing got written to disk. Why would
this
be? Is there a way to cause the programmatic behavior to match the manual
behavior (such that Save works)?

Thanks,
Eric



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why doesn't Workbook.Save work?

Are you suggesting that one of these other ways of getting the workbook
object will allow the save to work? Experimentally that doesn't seem to be
the case. None of these saves work either, in the programmatic scenario I
described. Do you mind sharing your thinking?

Thanks,
Eric

"JLGWhiz" wrote:

ActiveWorkbook.Save
Workbooks(1).Save
Workbooks("Book1").Save

"Eric Smith" wrote:

I'm working on an Excel add-in and found some unexpected behavior that I hope
someone might be able to explain to me.

The add-in handles the WorkbookBeforeClose event, and possibly saves the
Workbook. This works fine if the close is done manually -- that is, by
clicking the 'X' button. But if the close is done programmatically through a
call to Workbook.Close, then saving doesn't work.

I've written some VBA that replicates the problem (though to be clear, the
add-in is more complicated, doing more than just a simple save).

First is a class called XLEvents.

Code:

Private WithEvents xlApp As Excel.Application

Private Sub Class_Initialize()
Set xlApp = Application
End Sub

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
MsgBox "Closing"
Wb.Save
End Sub

I also have a macro that simply closes the Workbook. This isn't part of my
add-in's behavior -- it just illustrates the issue, and is something that
could happen if the user has their own macro, another add-in, etc.

Code:

Sub CloseWorkbook()
Application.ActiveWorkbook.Close
End Sub

This is the behavior I see:

Manual

1. Open the workbook.
2. Make a small edit.
3. Close the workbook by clicking the 'X'.
4. The "Closing" message box appears; click OK.
5. The workbook closes.

Programmatic

1. Open the workbook.
2. Make a small edit.
3. Run the "CloseWorkbook" macro.
4. The "Closing" message box appears; click OK.
5. Excel asks if I want to save.

#5 in the programmatic case is a symptom that the save didn't do anything in
this case. I can also verify that nothing got written to disk. Why would this
be? Is there a way to cause the programmatic behavior to match the manual
behavior (such that Save works)?

Thanks,
Eric

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Why doesn't Workbook.Save work?

I missed the part about programmatically closing the workbook. Will see if I
can find a way to get this to work.


"Tim Zych" <tzy---ch@NOSp@mE@RTHLINKDOTNET wrote in message
...
There's probably a more robust way to do this, but this should provide a
starting point. The key is to set Cancel = True.

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)

Cancel = True
MsgBox "Saving"
Wb.Save
xlApp.EnableEvents = False
Wb.Close False
xlApp.EnableEvents = True

End Sub

"Eric Smith" wrote in message
...
I'm working on an Excel add-in and found some unexpected behavior that I
hope
someone might be able to explain to me.

The add-in handles the WorkbookBeforeClose event, and possibly saves the
Workbook. This works fine if the close is done manually -- that is, by
clicking the 'X' button. But if the close is done programmatically
through a
call to Workbook.Close, then saving doesn't work.

I've written some VBA that replicates the problem (though to be clear,
the
add-in is more complicated, doing more than just a simple save).

First is a class called XLEvents.

Code:

Private WithEvents xlApp As Excel.Application

Private Sub Class_Initialize()
Set xlApp = Application
End Sub

Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
MsgBox "Closing"
Wb.Save
End Sub

I also have a macro that simply closes the Workbook. This isn't part of
my
add-in's behavior -- it just illustrates the issue, and is something that
could happen if the user has their own macro, another add-in, etc.

Code:

Sub CloseWorkbook()
Application.ActiveWorkbook.Close
End Sub

This is the behavior I see:

Manual

1. Open the workbook.
2. Make a small edit.
3. Close the workbook by clicking the 'X'.
4. The "Closing" message box appears; click OK.
5. The workbook closes.

Programmatic

1. Open the workbook.
2. Make a small edit.
3. Run the "CloseWorkbook" macro.
4. The "Closing" message box appears; click OK.
5. Excel asks if I want to save.

#5 in the programmatic case is a symptom that the save didn't do anything
in
this case. I can also verify that nothing got written to disk. Why would
this
be? Is there a way to cause the programmatic behavior to match the manual
behavior (such that Save works)?

Thanks,
Eric





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
Save As and Save Buttons Don't Work tthompson235 Excel Discussion (Misc queries) 1 June 3rd 09 02:44 AM
Workbook.Save triggers event Workbook_BeforeSave and things don't work! Dean Meyer Excel Programming 2 July 12th 06 06:00 PM
Everytime I save my workbook, my hyperlinks will no longer work. NUMBnut Excel Discussion (Misc queries) 0 December 30th 05 09:00 PM
I wish to save my Excell work in my work sheets CLC 37 Qld Excel Worksheet Functions 0 May 24th 05 10:56 AM
Is there away to keep "auto save" from jumping to the first work sheet in the work book? Marc New Users to Excel 2 April 21st 05 01:27 AM


All times are GMT +1. The time now is 06:39 AM.

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"