Excel AddIn - Is there a message before AddIn_Shutdown?
-
Tuesday, June 12, 2012 2:20 PM
Hi,
I created an Excel AddIn to encrypt/decrypt data in my worksheets. My problem is when the user unloads the AddIn, I get the _Shutdown message but I'm not able to encrypt my data. Is there some kind of message I can intercept as the AddIn is unloaded but before _Shutdown is called? I need to encrypt the data before the AddIn is unloaded and can't seem to figure out where/how to do it.
Thanks in advance,
Craig
All Replies
-
Tuesday, June 12, 2012 3:21 PM
Craig,
I think you should consider using WorkbookBeforeClose event instead of ThisAddIn_Shutdown event. The reason is if user closes workbook, ThisAddIn_Shutdown is not raised. So, later, when ThisAddIn_Shutdown is actually called, your data is not there in memory.
If I misunderstood the problem you are facing, please explain it further and I will try to offer a resolution if I could.
kr
-
Tuesday, June 12, 2012 3:30 PM
KR,
Thanks for the response. I'm actually using WorkbookBeforeClose as well. I keep a global variable to let me know in the _Shutdown event if the workbook is closing or not.
void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel) { g_bWorkbookClosing = true; }Ideally, I would like to do something like this:
private void ExcelAddIn_Shutdown(object sender, System.EventArgs e) { if(!g_bWorkbookClosing) { Encrypt(); } }The problem is, when I try to access the application, workbook or sheet, it throws an exception.
Does that make more sense?
Thanks again for your help,
Craig
-
Tuesday, June 12, 2012 4:47 PM
That is exactly what I mentioned. Shutdown event isn't raised when workbook closes. So, you should do your work inside WorkbookBeforeClose event handler when workbook data is still accessible.
-
Tuesday, June 12, 2012 6:58 PM
The problem I'm having is when the user unloads the AddIn, WorkbookBeforeClose event doesn't get called when the user unloads the AddIn. I'm looking for a solution for when the user unloads the AddIn, not closes the workbook, that part I have.
Thanks,
Craig
-
Wednesday, June 13, 2012 12:38 PM
I am sorry I didn't address the actual issue you mentioned.
Forgive my ignorance, what do you exactly mean by 'user unloads the Add-In'? Are you referring to the action under File -> Options -> Add-Ins -> View and Manage Office Add-ins?
kr
-
Wednesday, June 13, 2012 12:45 PM
Hi Kr,
Not at all, I don't think I explained it correctly.
Yes, that's exactly what I mean, when the user does File->Options->Add-Ins, then searches for COM Add-Ins, finds my AddIn, unchecks it and then clicks OK, I get the AddIn_Shutdown message but by then it's to late to encrypt my data. I'm looking for some message or way in between unloading my AddIn and the AddIn_Shutdown message. Is there such a way to do this?
Thanks again,
Craig
-
Wednesday, June 13, 2012 3:21 PM
I don't think there is a way to intercept the event of unloading Add-In. There is one event that is fired when Add-In is actually unloaded, but it is said to be of an infrastructure type. We are not supposed to use this event in our code. I hope someone form Microsoft will shed more light on it from this perspective.
I guess you may be able to work around by changing the visibility of one of the buttons that lead to this action (working with Backstage). Just hide that button from the user and add a custom button (that would look like the original button) and add a click event handler for that custom button where you can do whatever is necessary to manage your document and the data it contains.
kr
- Proposed As Answer by Leo_GaoModerator Monday, June 18, 2012 5:50 AM
- Marked As Answer by Leo_GaoModerator Thursday, June 21, 2012 1:41 AM
-
Wednesday, June 20, 2012 1:46 AMModerator
Hi,
Thanks for posting in the MSDN Forum.
As far as I know, there is a COMAddIn.Connect property which would be helpful. You can try this
if(!Application.COMAddIns(1).Connect) { encrypt(); }Furthermore,please refer to the following link for more information.
Best Regards,
Leo_Gao [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, June 20, 2012 2:22 PM
I don't think COMAddIn.Connect property is the solution to Craig's problem. He is looking for an event that he could use to detect 'Add-In is being off-loaded'.
kr

