I have a really weird problem and searching the net doesnt help. I have Windows Form app built in Visual Studio C# and some of the app users get an error when trying to export generated report to Excel (my app generates reports). This error comes and goes and
I dont get it when I run app myself.
Error is below. Could anyone suggest what can be the cause? As I never got this error myself debugging doesnt really help so I believe problem is not within the code.Error text:
System.Runtime.InteropServices.COMException (0x800A03EC): The specified file wasn't found.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Shapes.AddPicture(String Filename, MsoTriState LinkToFile, MsoTriState SaveWithDocument, Single Left, Single Top, Single Width, Single Height)
at WindowsFormsApp1.GroupAccOvwAll.btnExport1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
private void btnExport1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
this.Text = "Exporting...";
Range headersRow;
Range allData;
Range updateTimestamp;
saveFileDialog1.Filter = "Excel Documents (*.xls)|*.xls";
saveFileDialog1.FileName = "Guarantee Overview - " + dataGridView1.Rows[0].Cells[0].Value.ToString();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// Copy DataGridView results to clipboard
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
dataGridView1.MultiSelect = true;
dataGridView1.SelectAll();
copyAlltoClipboard();
object Missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
excel.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts
Excel.Workbook workbook = excel.Workbooks.Add(Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
worksheet = workbook.ActiveSheet;
worksheet.Name = "Summary";
worksheet.Cells.WrapText = false;
worksheet.Cells.Font.Name = "Text";
worksheet.Cells.Interior.Color = Color.FromArgb(255, 255, 255);
worksheet.Columns[2].NumberFormat = "@";
worksheet.Columns[4].NumberFormat = "@";
worksheet.Columns[5].NumberFormat = "@";
worksheet.Columns[6].NumberFormat = "@";
// Paste clipboard results to worksheet range
Excel.Range CR = (Excel.Range)worksheet.Cells[4, 1];
CR.Select();
worksheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
worksheet.Columns.AutoFit();
worksheet.Shapes.AddPicture(@"..x.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 180, 39);
allData = worksheet.UsedRange;
allData.Borders.LineStyle = XlLineStyle.xlContinuous;
Microsoft.Office.Interop.Excel.Range ur = worksheet.UsedRange;
Microsoft.Office.Interop.Excel.Range r = worksheet.Cells[4, ur.Columns.Count];
headersRow = worksheet.Range["A4", r];
headersRow.Cells.Font.Bold = true;
headersRow.Cells.Font.Color = Color.FromArgb(255, 255, 255);
headersRow.Cells.Interior.Color = Color.FromArgb(0, 055, 085);
worksheet.Cells[2, 5] = worksheet.Cells[5, 20];
worksheet.Cells[2, 5].Font.Bold = true;
worksheet.Cells[2, 4] = "Report date:";
worksheet.Cells[2, 4].Font.Bold = true;
worksheet.Cells[2, 4].HorizontalAlignment = XlHAlign.xlHAlignRight;
updateTimestamp = worksheet.Range["T5", "T5"].EntireColumn;
updateTimestamp.Delete();
// Delete blank column A and select cell A1
Excel.Range delRng = worksheet.get_Range("A:A").Cells;
delRng.Delete(Type.Missing);
worksheet.get_Range("A1").Select();
// Save the excel file under the captured location from the SaveFileDialog
workbook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, Missing, Missing, Missing, Missing, Excel.XlSaveAsAccessMode.xlExclusive, Missing, Missing, Missing, Missing, Missing);
excel.DisplayAlerts = true;
workbook.Close(true, Missing, Missing);
excel.Quit();
releaseObject(worksheet);
releaseObject(workbook);
releaseObject(excel);
// Clear Clipboard and DataGridView selection
Clipboard.Clear();
dataGridView1.ClearSelection();
// Open the newly saved excel file
if (File.Exists(saveFileDialog1.FileName))
System.Diagnostics.Process.Start(saveFileDialog1.FileName);
}
}
private void copyAlltoClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occurred while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
this.Close();
}