0

have problem with interop and editing existing excel file. Excel app doesn't want to close. If I set code, when I edit cells, as comment, rest of code work as I need - Excel open and after print dialog close.

Excel.Application excelApp = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorksheet = null;

string workbookPath = "";
if (RBType1.Checked == true) { workbookPath = Path.GetFullPath("Excel/Mesto.xls"); }
else if (RBType2.Checked == true) { workbookPath = Path.GetFullPath("Excel/Ulehly.xls"); }
else if (RBType3.Checked == true) { workbookPath = Path.GetFullPath("Excel/Spolecenstvi.xls"); }
else if (RBType4.Checked == true) { workbookPath = Path.GetFullPath("Excel/Vlastnici.xls"); }

excelApp = new Excel.Application();
excelApp.Visible = true;

excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets.get_Item(1);


excelWorksheet.Cells[4, "J"] = RequisitionNumberBox.Text;
excelWorksheet.Cells[9, "C"] = ApplicantBox.Text;
excelWorksheet.Cells[9, "G"] = StreetBox.Text;
excelWorksheet.Cells[9, "J"] = CPBoxC.Text;
excelWorksheet.Cells[10, "J"] = CBBox.Text;
excelWorksheet.Cells[11, "D"] = CTBox.Text;
excelWorksheet.Cells[11, "G"] = ContactPersonBox.Text;
excelWorksheet.Cells[14, "B"] = RepairBox.Text;
excelWorksheet.Cells[20, "B"] = ItemBox.Text;
if (PMOption1.Checked == true) { excelWorksheet.Cells[23, "D"] = "X"; }
if (PMOption2.Checked == true) { excelWorksheet.Cells[24, "D"] = "X"; }
excelWorksheet.Cells[23, "F"] = IssueDate.Text;

excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show();

excelWorkbook.Close(false, Type.Missing, Type.Missing);
excelApp.Quit();

releaseObject(excelWorksheet);
releaseObject(excelWorkbook);
releaseObject(excelApp);

releaseObject code (found this on internet)

try
{
     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
     obj = null;
}
catch (Exception ex)
{
     obj = null;
     MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
     GC.Collect();
}

How I should edit this code, that it close Excel when program edits cells?

  • 1
    The is a VFAQ, read [this](http://stackoverflow.com/a/17131389/17034). Remove the releaseObject() calls, move the GC.Collect() call to the method that calls this code. – Hans Passant Jul 25 '13 at 12:13
  • This dont work :/ Edit main post how it looks now – Lukáš Velecký Jul 25 '13 at 13:11
  • Maybe I dont understand your help, can you help me more? – Lukáš Velecký Jul 25 '13 at 13:15
  • Check my comment again: **remove the releaseObject() calls**. They just don't work. There are other objects that you can't see. Now you'll have some hope of putting the GC.Collect() call in the right place. Which is in **the method that calls this code**. Don't know how to make that simpler to understand. – Hans Passant Jul 25 '13 at 13:24
  • Ho I got it :) I dont understand second part of your comment so well. – Lukáš Velecký Jul 25 '13 at 13:33

0 Answers0