Home » SQL ServerRSS

WorkbookBeforeSave event SaveAsUI always true

I have a VSTO excel addin developed in .net 3.5 and recently ported to target .net 4

I have wired up the Application.WorkbookBeforeSave event during addin startup.

What I am now seeing is that the SaveAsUI parameter is always true whenever this event fires. Previously, in 3.5 it would be true only when you'd expect, based on whether the "save as" dialog was to be shown.

Anyone seen anything like this?

 

 

8 Answers Found

 

Answer 1

Hello Roger,

Thanks for posting. I read your post carefully. To get a better understanding on this issue, would you mind clarifying some points for us?

Which version of Office and Visual Studio you are involved in? You are working with an document-level project or an application  one? Which button you pressed to trigger WorkbookBeforeSave event?

Based on my observation, if we click SaveAs button to save  as this workbook, it also will trigger this event, and the SaveAsUI will be always true. If we click Save button, SaveAsUI might be true  the first time. For this point, if you have any concern for this, please feel free to follow up.

We are looking forward to you reply. Have a nice day!

Best regards,
Bessie Zhao - MSFT
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
 

Answer 2

Thanks for your response.  In answer to your questions...

This is office 2007 on a variety of OS's using VS2010 to build an application  level addin.

To reproduce, create a new project in vs2010 using the template: Office / 2007 / Excel 2007 Add-in

Use the default target  Framework of .net 4 or the .net 4 client profile.

Replace the contents of ThisAddin.cs with the code at the end of this post.

Run the addin  by pressing F5 or however you like to do it.

You have a blank workbook. 

Press ctrl-s to save  it.   The save as dialog  appears, and you see that the diagnostic message indicates that the saveAs parameter  is True, which is what you would expect  it to be.   Save the file someplace.

Modify a cell in the workbook.  Then press ctrl-s again.  Now, the document is saved with no save as dialog appearing, as you'd expect. However, note that the diagnostic message indicates that the saveAs parameter is True. We expect false in this case, since no saveAs dialog is shown.  

Repeat as often as you like, using Ctrl-S, the Save button in the QAT, and the Save button in the Application menu, always the same result - no dialog but diagnostic message say saveAs is True.

Now change the project to target .net 3.5 (don't forget to do build/clean first!) and repeat the steps above. Now, the behavior of the diagnostic message is as you would expect - true  the first time when the save as dialog is shown, false thereafter (unless of course you actually do a save as to a new file)

Not shown, but also true, is the behavior when the save operation is initiated from code, using the Workbook.Save method. The parameter is always True, even when saving with no saveAs UI showing.

 

Hope that clarifies. Sample addin code follows...

---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Diagnostics;

namespace BeforeSaveEvent
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.WorkbookBeforeSave += Application_WorkbookBeforeSave;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        private void Application_WorkbookBeforeSave(Excel.Workbook workbook, bool saveAs, ref bool cancel)
        {
            Trace.WriteLine("Application_WorkbookBeforeSave saveAs = " + saveAs);
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
       
        #endregion
    }
}

 

Answer 3

Hello again Roger,

Thanks for the further information. By following the steps, I was able to reproduce the issue you are seeing. I also have created an Excel Add-in under Office 2010 and Visual Studio 2010. By change the version of .NET Framework, the scenario also happens. So I think this issue is probably related to be .NET Framework 4.0.

I have reported a feedback through Visual Studio & .NET Connect site:
http://connect.microsoft.com/VisualStudio/feedback/details/573270/saveasui-in-workbookbeforesave-event-hander-is-always-true. This issue will be routed to the product unit who works on this specific feature area for triage and resolution. I’ll paste all responses from the product unit in this thread. And you also can click the link and add the feedback to your watch list (you need to log in using your account) so that you can get informed as soon as product units respond. It may be some time before we get response from product unit. Please be patient.

Best regards,
Bessie Zhao - MSFT
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.

 

Answer 4

Hello again Roger,

I am writing to check the status of the issue on your side. If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.

Best regards,
Bessie Zhao - MSFT
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.

 

Answer 5

I see that you submitted this as a bug to the connect site, so I guess I'm waiting to see how that goes.  The problem puts us in a bit of a hard place - we can kind of work around it but not totally.  If there's anything you can do to expedite it with the product group, that would be the most valuable thing.

Thanks again

rsr

 

Answer 6

Hi Roger,

We have found this issue to be a bug in .Net Framework.

There are some workarounds you can try:

1. Disable Embedding the Microsoft.Office.Interop.Excel.dll in the vsto  customization by Setting the EmbedInteropType property to false for Microsoft.Office.Interop.Excel.dll under References in Solution Explorer.
2. Or use the VBAInterop feature of VSTO to handle the event  in VBA and then call a function in VSTO customization with the parameters passed to the VBA event. For more information about VBA Interop in VSTO customization please have a look at http://blogs.msdn.com/b/bethmassi/archive/2009/11/05/interop-between-vba-and-visual-studio-office-solutions-vsto.aspx

Thanks,

 

Answer 7

Well, the typelib embedding feature is the reason we moved to .net 4 in the first place, so we won't be disabling it.

The VBAInterop approach sounds like it might be workable for us, so I'm going to try and pursue that.

Question though - should there be some updates about this attached to that connect site?

Thanks for your help here

 

Answer 8

It looks like hooking up a VBA event  handler for Application.BeforeWorkbookSave and then having that forward the call into our VSTO addin  does the trick as a workaround until the issue can be properly fixed in the framework.

Thank you very much for following this through and guiding it through the connect site and process.

 

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter