I have created a VSTO Excel Add-In with C# which uses a .xlsx as a configuration file.
The configuration file is opened within RibbonDropDown_ItemsLoading
by
private void RibbonDropDown_ItemsLoading(object sender, RibbonControlEventArgs e)
{
//Keep the config template always open
AddInFunctions.chkConfigTemplateState();
\\--> Excel.Application.Workbooks.Open(UserXLSConfigTemplateFilePath).Windows[1].Visible = false
//add list of names from tmpConfigNames
rbnDropDown.Items.Clear();
//add new items to drpDwnSelectNoteTemplate from AddInFunctions.tmpConfigNames()
foreach (string s in AddInFunctions.tmpConfigNames())
{
RibbonDropDownItem rdi = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
rdi.Label = s;
Globals.Ribbons.Ribbon1.rbnDropDown.Items.Add(rdi);
}
}
This process however intervense with Excels startup procedure given that the user opens Excel by clicking on the Excel icon. Right after the last brace of RibbonDropDown_ItemsLoading
When Option toggle "Show the Start screen when this application starts":
--> is checked: opening my config file invisible prevents the "Start screen" to come up
--> is unchecked: Excel crashes
eventually I should load the file at a differnt step?
Ultimatly the goal is to respect the start up options:
- If start screen is usually opened it should still be presented to the user
- If start screen is usually not opened, present the user with a blank Excel
- If user is used to seeing excel opening with a new workbook this shouldn't change too etc.
Tests:
- inactivate both steps by //: successfully loading Excel & AddIn
- don't hide workbook --> set visible = true: same crash behaviour
- don't add the items just load template: same crash behaviour
- remove application screenupdate from openTemplate Function: same crash behaviour
EventLog:
The program '[768] excel.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.
EventData
1881252799848305173
4
APPCRASH
Not available
0
excel.exe
16.0.15225.20288
62a3df4b
mso98win32client.dll
0.0.0.0
62a31f70
c0000005
000000000001a267
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WER9130.tmp.WERInternalMetadata.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\ReportArchive\AppCrash_excel.exe_ee94e2c752da07819d933041028d3f5d2787_fd09608a_4640236e-8eec-4fed-8501-452bbbe40250
0
cf4dbc3b-9995-40b9-a9c6-7ad4cbf1ebce
268435456
a88fa5fe3d21b47d3a1b8d7443aad215
0
CodePudding user response:
The problem occured because of the DropDown event trigger was set of while the ribbon was loading.
When one double clicks a ribbon component in the Designer View, VS will add an event for that component presenting the developer with a prepared line of code like this one:
private void rbnDropDown_ItemsLoading(object sender, RibbonControlEventArgs e)
{
}
what I didn't see however is the line of code that is being added to the ribbon.designer.cs:
this.rbnDropDown.ItemsLoading = new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.rbnDropDown_ItemsLoading);
Now while the AddIn is still loading the event will get triggered which makes no sense really and provokes surpressing the start screen and causes excel to crash if the start screen option is disabled.
Note that I do not fully understand what causes the behaviour but removing that line of code from Ribbon.Designer.cs has solved the issues described in my Question.
CodePudding user response:
The configuration file is opened within RibbonDropDown_ItemsLoading by
The ribbon callbacks are not appropriate place for such things. The Office applications may not be initialized to make any operations at this point of time. I'd suggest using the Startup
callback of your ThisAddin
class implementation.