Exporting DataGridView to Excel using Interop crashes Outlook 2010

I have a Windows Form application with DataGridViews, on two of these grids I have an export button that allows the user to export the data in the grid to an excel file.

The export seems to work just fine, the EXCEL.EXE process exited correctly after the event was fired and the excel file generated is perfect. But after exporting one of my datagridviews to excel, if I try to send a new email or reply to an email message in Outlook 2010, Outlook stops working.

After several attempts, I get the message "Outlook has a serious problem with the Microsoft Outlook social add-in add-in ..." enter image description here

I'm pretty sure it is my export function in my application that is causing this because Outlook behaves fine until I export immediately after it crashes if I try to send an email. I can duplicate this as I see fit and I have duplicated this result on other workstations as well.

My Outlook and Outlook on other workstations that have experienced this have a custom add-in. This add-in uses the following namespaces:

using Office = Microsoft.Office.Core;
using RibbonStuff = Microsoft.Office.Tools.Ribbon;
using Outlook = Microsoft.Office.Interop.Outlook;

      

Here is the code from my export event.

using Excel = Microsoft.Office.Interop.Excel;

private void exportUpdateGridToExcel()
{
    Excel.Application xlApp = null;
    Excel.Workbook xlBook = null;
    Excel.Worksheet xlSheet = null;
    try
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents (*.xls)|*.xls";
        sfd.FileName = "Inventory_Adjustment_Export.xls";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            // Copy DataGridView results to clipboard
            copyAlltoClipboard(dgvItems);

            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();

            xlApp.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts
            xlBook = xlApp.Workbooks.Add(misValue);
            xlSheet = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);

            // Format column D as text
            Excel.Range rng = xlSheet.get_Range("D:D").Cells;
            rng.NumberFormat = "@";

            // Paste clipboard results to worksheet range
            Excel.Range CR = (Excel.Range)xlSheet.Cells[1, 1];
            CR.Select();
            xlSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

            // Delete blank row header column and select cell A1
            Excel.Range delRng = xlSheet.get_Range("A:A").Cells;
            delRng.Delete(Type.Missing);
            xlSheet.get_Range("A1").Select();

            // Save excel file and capture the location from the SaveFileDialog
            xlBook.SaveAs(sfd.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            // Clear Clipboard and select "Item" cell
            Clipboard.Clear();
            dgvItems.ClearSelection();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        xlBook.Close(false);
        xlApp.Quit();
    }
}

private void copyAlltoClipboard(DataGridView dv)
{
    dv.SelectAll();
    DataObject dataObj = dv.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);
}

      

+3


source to share





All Articles