I created a VSTO addin which will perform the following actions
Step 1. In addin startup I'm creating a combo box in an excel cell.
Step 2. Added a delete button in ribbon control and on click of button delete the combo box added in step 1.
Step 3. Add a button for save and Save the workbook to one drive on click.
On click of save excel is getting crashed and restart excel. The save is working fine when selecting any local drive. It's crashing only if we select to save in one drive. Any help will be appreciated?
Here I'm attaching the code snippet.
public partial class ThisAddIn
{
static Excel.Worksheet Sheet;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Sheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
var oleObject = CreateComboBox();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
/// <summary>
/// Delete the page filter added
/// </summary>
public static void DeletePageFilters()
{
for (int i = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Shapes.Count; i > 0; i--)
{
Excel.Shape oleObj = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Shapes.Item(i);
if (oleObj.Name.Contains("Combo"))
{
try
{
oleObj.Delete();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oleObj);
}
catch (Exception ex) { }
}
}
}
/// <summary>
/// Create the page filter
/// </summary>
public static Excel.Shape CreateComboBox()
{
Excel.Shape oCombo = null;
try
{
Excel.Range cell = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Range["A1", "A1"];
oCombo = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Shapes.AddOLEObject("Forms.ComboBox.1", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
cell.Left, cell.Top, 40, cell.Height);
}
catch (System.Exception ex)
{
}
return oCombo;
}
Code to save the workbook
private void Save_Click(object sender, RibbonControlEventArgs e)
{
try
{
Globals.ThisAddIn.Application.ActiveWorkbook.Save();
}
catch (Exception)
{
throw;
}
}
Event Log
Faulting application name: EXCEL.EXE, version: 16.0.13127.21624, time stamp: 0x6093b3ca
Faulting module name: VBE7.DLL, version: 0.0.0.0, time stamp: 0x5ff762b0
Exception code: 0xc0000005
Fault offset: 0x0000000000250081
Faulting process id: 0x69c4
Faulting application start time: 0x01d76368d4ad66cc
Faulting application path: C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE
Faulting module path: C:\Program Files\Common Files\Microsoft Shared\VBA\VBA7.1\VBE7.DLL
Report Id: 99cb77c6-5e34-4472-a469-3295d1136167
Faulting package full name:
Faulting package-relative application ID: