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

Hi all:

In excel 2003, I am setting some CustomProperties for the active worksheet.
However, before the workbook is closed, I would like to delete all the
custom properties. Does anyone know how I can find out when the workbook is
closing, and in which event I have to write my code for. Please note,
ultimately, my code would become an Add-in, and would not be tied to any
workbook in particular. Thanks for your help.

Bob


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Workbook closing


Put the following code in the ThisWorkbook module (it must be in the
ThisWorkbook module):

Private WithEvents XLApp As Excel.Application

Private Sub Workbook_Open()
Set XLApp = Application
End Sub

Private Sub XLApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
Dim WS As Worksheet
Dim N As Long
For Each WS In Wb.Worksheets
With WS.CustomProperties
For N = .Count To 1 Step -1
.Item(N).Delete
Next N
End With
Next WS

End Sub


With this code, whenever any workbook is closed, all of the custom
properties are removed from each worksheet in the workbook.

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
The San Diego Project Group, LLC
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)



On Tue, 23 Sep 2008 15:51:36 -0700, "Bob" wrote:

Hi all:

In excel 2003, I am setting some CustomProperties for the active worksheet.
However, before the workbook is closed, I would like to delete all the
custom properties. Does anyone know how I can find out when the workbook is
closing, and in which event I have to write my code for. Please note,
ultimately, my code would become an Add-in, and would not be tied to any
workbook in particular. Thanks for your help.

Bob

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Workbook closing

It has a before close event. I am not sure how you would capture it for an
add-in.
Here is the Help file description.

BeforeClose Event
SpecificsOccurs before the workbook closes. If the workbook has been
changed, this event occurs before the user is asked to save changes.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Cancel False when the event occurs. If the event procedure sets this
argument to True, the close operation stops and the workbook is left open.





"Bob" wrote:

Hi all:

In excel 2003, I am setting some CustomProperties for the active worksheet.
However, before the workbook is closed, I would like to delete all the
custom properties. Does anyone know how I can find out when the workbook is
closing, and in which event I have to write my code for. Please note,
ultimately, my code would become an Add-in, and would not be tied to any
workbook in particular. Thanks for your help.

Bob



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default Workbook closing

Thanks guys for your great inputs. That is true. I am not sure how to
handle the save? For example, if the user decides to close the file, and I
delete all the information before he gets the message "whether he wants to
save the changes", and then the user decides to cancel the close, I loose
all the information. I guess, the only thing I can do is the following
(which I have no idea on how to even start on)
1- If the user decides to close the file, the program gives him the option
to save
2- if he chooses Cancel, for my code not to do anything.
3- if he chooses Yes the program should save his work, then for my program
to delete the custom properties, and then save the workbook again (to get
rid of them), and then close the workbook.
4- if the user chooses No, I have no idea what to do. If, since the opening
of the workbook, the workbook was not saved, then this is easy, the workbook
should close, and the custom properties are deleted. That is fine.
However, if the workbook was saved at some point, my custom properties are
also saved with it. Then the user does some work, and decides to abandon
the changes and close the file without saving. In this case, I have no idea
how to delete my custom properties.

Bob

"JLGWhiz" wrote in message
...
It has a before close event. I am not sure how you would capture it for
an
add-in.
Here is the Help file description.

BeforeClose Event
SpecificsOccurs before the workbook closes. If the workbook has been
changed, this event occurs before the user is asked to save changes.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Cancel False when the event occurs. If the event procedure sets this
argument to True, the close operation stops and the workbook is left open.





"Bob" wrote:

Hi all:

In excel 2003, I am setting some CustomProperties for the active
worksheet.
However, before the workbook is closed, I would like to delete all the
custom properties. Does anyone know how I can find out when the workbook
is
closing, and in which event I have to write my code for. Please note,
ultimately, my code would become an Add-in, and would not be tied to any
workbook in particular. Thanks for your help.

Bob





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default Workbook closing

Thanks Chip. Your code helped. However, I have put some comment on it in
my reply to JLGWhiz. thanks for your help.

Bob

"Chip Pearson" wrote in message
...

Put the following code in the ThisWorkbook module (it must be in the
ThisWorkbook module):

Private WithEvents XLApp As Excel.Application

Private Sub Workbook_Open()
Set XLApp = Application
End Sub

Private Sub XLApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
Dim WS As Worksheet
Dim N As Long
For Each WS In Wb.Worksheets
With WS.CustomProperties
For N = .Count To 1 Step -1
.Item(N).Delete
Next N
End With
Next WS

End Sub


With this code, whenever any workbook is closed, all of the custom
properties are removed from each worksheet in the workbook.

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
The San Diego Project Group, LLC
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)



On Tue, 23 Sep 2008 15:51:36 -0700, "Bob" wrote:

Hi all:

In excel 2003, I am setting some CustomProperties for the active
worksheet.
However, before the workbook is closed, I would like to delete all the
custom properties. Does anyone know how I can find out when the workbook
is
closing, and in which event I have to write my code for. Please note,
ultimately, my code would become an Add-in, and would not be tied to any
workbook in particular. Thanks for your help.

Bob



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
Closing a workbook from a macro doesn't close the workbook Dave P Excel Programming 2 July 10th 07 06:16 PM
Closing a possible Workbook kyle Excel Programming 2 July 11th 06 08:14 PM
closing a workbook oercim Excel Programming 0 March 24th 06 01:27 PM
Closing Hidden Workbook when Active Workbook is Closed SusanK521 Excel Programming 5 September 24th 05 12:27 AM
closing excel after closing a workbook CWalsh[_2_] Excel Programming 3 January 21st 04 03:33 PM


All times are GMT +1. The time now is 03:04 PM.

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"