-1

I use a savefiledialog in the application to save the file. If the same filename already exists I get a popup asking if i want to replace or not. If i give no I get unexpected system exception. Below is part of code

string fname1 = "";

saveFileDialog.Title = "Save the Proofer Report";
saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.InitialDirectory = "MyDocuments";
saveFileDialog.FileName = "Proofer Report";
aveFileDialog.AddExtension = true;
saveFileDialog.ShowHelp = true;

// saveFileDialog.ShowDialog();

Invoke((Action)(() => { saveFileDialog.ShowDialog(); }));
fname1 = saveFileDialog.FileName;


                xlWorkBook.SaveAs(fname1, Excel.XlFileFormat.xlWorkbookNormal,       misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 //system exception during save as
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

stack trace

at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) at ProoferXML.MainForm.ProcessDocument(BackgroundWorker worker, DoWorkEventArgs e) in D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:line 665 at ProoferXML.MainForm.prooferWorker_DoWork(Object sender, DoWorkEventArgs e) in D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:line 1457

While Saving the excel using workbook.saveAs getting the Exception from HRESULT:

user1665707
  • 495
  • 3
  • 10
  • 24

1 Answers1

0

Most likely you have to pay attention to threading model you are using. It is recommended to check InvokeRequired before calling Invoke. So your code should look like:

 if (InvokeRequired)
 {
   Invoke((Action)(() => { saveFileDialog.ShowDialog(); }));
 }
 else
 {
   saveFileDialog.ShowDialog();
 }

Some more useful reading Invoke Invoke(Delegate)

Community
  • 1
  • 1
Max Markov
  • 784
  • 10
  • 22
  • Am getting error during save as --- xlWorkBook.SaveAs(fname1, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue) – user1665707 Sep 06 '13 at 07:23
  • While Saving the excel using workbook.saveAs getting the Exception from HRESULT: – user1665707 Sep 06 '13 at 07:42
  • you may try to use Application.DisplayAlerts = false; before calling SaveAs to prevent overwrite warning generating by Excel. – Max Markov Sep 06 '13 at 08:09