I got a program which asks for the filename and place where to save it with saveFileDialog1
. Tho it generates an extra file of 0 bytes with exactly the name name. The type of file is File
and when I open it, it tells me it isnt there. I can see this file in my Recent files
but cant find it at the place I saved the file. This is my code:
private void MaakExcelFile()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//string zonderspecialchars = RemoveSpecialCharacters(saveFileDialog1.FileName);
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add();
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
// string zonderspecialchars = RemoveSpecialCharacters(filenaam);
try
{
xlWorkBook.SaveAs(@"" + saveFileDialog1.FileName, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception e)
{ throw e; }
}
}
Anyone any idea why this happens? This is how my recent files looks like: https://i.stack.imgur.com/TA4Mf.jpg
It must be happening in MakeExcelFile(); since xlWorkBook.SaveAs will only save the workbook and does not create any additional files.
Edit: try adding workbook.close(). xlapp.Close() after saveas. You may just be keeping that workbook open...
Edit2: Remove your SaveFileDialog and hardcode a path to save the excel file. See if this only saves one file.
Edit3: Hmm.. should do the same thing this way but something is running twice. Either embed the saveasdialog in your try{} block and only saveas with it or switch to a folderbrowserdialog and name the file in the code.