none
Creating a CommandBar control on a context menu crashes Excel 2016 RRS feed

  • Question

  • Hello,

    Consider a VSTO add-in creating a custom ComandBar button in the Cells context menu:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Office.CommandBars cmdBars = this.Application.CommandBars;
        Office.CommandBar cmdBar = cmdBars["Cell"];
        Office.CommandBarControls cmdBarControls = cmdBar.Controls;
        Office.CommandBarButton cmdButton = cmdBarControls.Add(Type: Office.MsoControlType.msoControlButton, Id: 1, Temporary: true) as Office.CommandBarButton;
        cmdButton.BeginGroup = false; // <-- this line crashes Excel
        cmdButton.Caption = "test button";
        cmdButton.Enabled = true; 
        cmdButton.Visible = true;
        Marshal.ReleaseComObject(cmdButton); cmdButton = null;
        Marshal.ReleaseComObject(cmdBarControls); cmdBarControls = null;
        Marshal.ReleaseComObject(cmdBar); cmdBar = null;
        Marshal.ReleaseComObject(cmdBars); cmdBars = null;
    }
    

    We have at least two machines on which the code above crashes Excel 2016 build 8730.2046 32bit with somewhat different symptoms. In both cases the crash occurs on the same code line:

    Application: EXCEL.EXE
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException
       at Microsoft.Office.Core._CommandBarButton.set_BeginGroup(Boolean pvarfBeginGroup)
       at ExcelAddIn10.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e)
       at Microsoft.Office.Tools.AddInImpl.OnStartup()
       at Microsoft.Office.Tools.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup()
       at Microsoft.Office.Tools.AddInBase.OnStartup()
       at ExcelAddIn10.ThisAddIn.FinishInitialization()
       at Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization()
       at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)
       at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()
    

    Machine #1. On this machine, the faulting module’s version is 10.0.16299.15. We register the add-in and start Excel. Nothing else is required to reproduce the crash. Event Viewer shows this info on that machine:

    Faulting application name: excel.exe, version: 16.0.8730.2046, time stamp: 0x5a06f6a8
    Faulting module name: KERNELBASE.dll, version: 10.0.16299.15, time stamp: 0x2cd1ce3d
    Exception code: 0xe0434352
    Fault offset: 0x001008b2
    Faulting process id: 0x1a28
    Faulting application start time: 0x01d35ec00ed5ca65
    Faulting application path: C:\Program Files (x86)\Microsoft Office\root\Office16\excel.exe
    Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll

    Restarting Excel shows that Excel thinks the VSTO add-in above is responsible for the crash: Excel suggests disabling it.

    Machine #2. On this machine, the faulting module’s version is 10.0.15063.674. We register the add-in, start Excel, enable Analysis ToolPak as well as Analysis ToolPak – VBA (see the Add-ins dialog), restart Excel and get the crash:

    Faulting application name: EXCEL.EXE, version: 16.0.8730.2046, time stamp: 0x5a06f6a8
    Faulting module name: KERNELBASE.dll, version: 10.0.15063.674, time stamp: 0x6d16dd24
    Exception code: 0xe0434352
    Fault offset: 0x000eb872
    Faulting process id: 0x267c
    Faulting application start time: 0x01d35ebdf52b9431
    Faulting application path: C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE
    Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
    Report Id: d66a7f54-58f0-4080-be3b-7dcb7012d36b
    Faulting package full name: 
    Faulting package-relative application ID:

    Restarting Excel shows that Excel declares 1) the VSTO add-in and 2) atpvbaen.xlam responsible for the crash.

    A slightly different variant of the same crash occurs if we use a COM add-in (instead of the VSTO add-in) and create the commandbar button above in the IDTExtensibility2.AddinStartupComplete method. In this case, the faulty module name is ucrtbase.dll:

    Faulting application name: excel.exe, version: 16.0.8730.2046, time stamp: 0x5a06f6a8
    Faulting module name: KERNELBASE.dll, version: 10.0.16299.15, time stamp: 0x2cd1ce3d
    Exception code: 0xe0434352
    Fault offset: 0x001008b2
    Faulting process id: 0x1a28
    Faulting application start time: 0x01d35ec00ed5ca65
    Faulting application path: C:\Program Files (x86)\Microsoft Office\root\Office16\excel.exe
    Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
    Report Id: 1a50c41f-e62d-49ce-a151-f155b7ba5301
    Faulting package full name: 
    Faulting package-relative application ID:
    On machine#2 (10.0.15063.674), Excel marks atpvbaen.xlam as a single add-in responsible for the crash; it doesn’t suggest disabling our add-in.


    Also, we reproduce the issue on the 64bit version of the same Excel build with the COM add-in. On that machine, crashes follow the patterns of machine#1. No actual crash information is available for that machine at the moment. Please let me know if it is required.

    Finally, our customer gets this crash on Excel build 8625.2064 32bit; on machine#2, we have the same exception code:

    Faulting application name: EXCEL.EXE, version: 16.0.8625.2064, time stamp: 0x59e70c3a
    Faulting module name: ucrtbase.dll, version: 10.0.10240.16384, time stamp: 0x559f3aa5
    Exception code: 0xc0000409
    Fault offset: 0x0007d84b
    Faulting process id: 0xfec
    Faulting application start time: 0x01d35f160c5db4fe
    Faulting application path: C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE
    Faulting module path: C:\Windows\SYSTEM32\ucrtbase.dll
    Report Id: 6360b314-af56-46be-a94d-3ba1bfe93d65
    Faulting package full name: 
    Faulting package-relative application ID: 
    Please let me know if you need more information. 

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, November 16, 2017 11:30 AM

Answers

  • Hello,

    I could reproduce the issue in Excel build 8730.2046 and build 8325.2121.

    In build 8325.2121, Excel throws exception when accessing button's properties:

    System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'

    In build 8730.2046, Excel would crash if we access properties of the new created command bar button before opening any workbooks and atpvbaen.xlam (Analysis ToolPak – VBA) is loaded at the same time.

    The issue does not exist in build 8201.2200 (Deferred channel). I suggest you submit a feedback via File -> Feedback.

    To work around, you may unload the Analysis ToolPak – VBA before adding the command bar or setting its properties at NewWorkbook or WorkbookOpen event.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Andrei Smolin Wednesday, November 22, 2017 1:31 PM
    Friday, November 17, 2017 8:37 AM
    Moderator

All replies

  • I should have stressed that using a COM add-in produces a different faulting module: ucrtbase.dll instead of KERNELBASE.dll in case of a VSTO add-in.

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, November 16, 2017 11:44 AM
  • "In both cases the crash occurs on the same code line:"

    No idea what's going on there but what happens if you omit .BeginGroup = False, it's default so I assume redundant. Does the same error then occur applying the next property..? 

    FWIW, I've had my own VBA addins which have worked fine for years, and even sometimes MS addins, falsely 'accused' of being responsible some crash at startup, and then prompted to permanently disable them.

    [Edit] Another thought - if your startup code is at Excel startup and before any workbooks exist, is there any difference if you defer adding the control until at least one workbook exists.
    Thursday, November 16, 2017 7:01 PM
    Moderator
  • Hello,

    I could reproduce the issue in Excel build 8730.2046 and build 8325.2121.

    In build 8325.2121, Excel throws exception when accessing button's properties:

    System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'

    In build 8730.2046, Excel would crash if we access properties of the new created command bar button before opening any workbooks and atpvbaen.xlam (Analysis ToolPak – VBA) is loaded at the same time.

    The issue does not exist in build 8201.2200 (Deferred channel). I suggest you submit a feedback via File -> Feedback.

    To work around, you may unload the Analysis ToolPak – VBA before adding the command bar or setting its properties at NewWorkbook or WorkbookOpen event.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Andrei Smolin Wednesday, November 22, 2017 1:31 PM
    Friday, November 17, 2017 8:37 AM
    Moderator
  • Thank you, Peter!

    Thank you, Celeste!


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Wednesday, November 22, 2017 1:31 PM
  • I suggest you submit a feedback via File -> Feedback.

    Done. Thank you again!


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Wednesday, November 22, 2017 1:39 PM