So, I have created an Excel file in my same wpf Application on the Main Window. Now, I want to edit that file so that I can add another worksheet and place some data on it.
The file creates itself just fine and puts all the necessary data on it, but when I try to call it again a COMExcpetion comes up saying that it can't found the file, but the file IS there !!
I think it is nothing mayor, but thank you so much for any help !
Here is the code:
private void Nextday_Click(object sender, RoutedEventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Open(@"C:Users\Public\Documents\Report.xls");
Worksheet ws2 = (Worksheet)wb.Worksheets[2];
ws2.Name = "Part Function";
object misValue = System.Reflection.Missing.Value;
int i = 1;
ws2.Cells[i, 1] = "Part Function Auto";
i++;
ws2.Cells[i, 1] = "Problem Reported in TestID#:";
i++;
ws2.Cells[i, 1] = "Problem is:";
i++;
ws2.Cells[2, 2] = testBox.Text;
wb.Save();
wb.Close();
}
And here is how I created the Excel file in the first place.
private void submit_Click(object sender, RoutedEventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
ws.Name = "Personal Info";
wb.Sheets.Add(After: wb.Sheets[wb.Sheets.Count]);
object misValue = System.Reflection.Missing.Value;
TextRange programTxt = new TextRange(program.Document.ContentStart, program.Document.ContentEnd);
TextRange dateTxt = new TextRange(date.Document.ContentStart, date.Document.ContentEnd);
TextRange vinTxt = new TextRange(vin.Document.ContentStart, vin.Document.ContentEnd);
TextRange dandrTxt = new TextRange(dandr.Document.ContentStart, dandr.Document.ContentEnd);
TextRange cdsidTxt = new TextRange(cdsid.Document.ContentStart, cdsid.Document.ContentEnd);
TextRange milestoneTxt = new TextRange(milestone.Document.ContentStart, milestone.Document.ContentEnd);
TextRange alertTxt = new TextRange(alert.Document.ContentStart, alert.Document.ContentEnd);
int i = 1;
ws.Cells[i, 1] = "Program:";
i++;
ws.Cells[i, 1] = "Date:";
i++;
ws.Cells[i, 1] = "VIN:";
i++;
ws.Cells[i, 1] = "D & R:";
i++;
ws.Cells[i, 1] = "CDS ID:";
i++;
ws.Cells[i, 1] = "Milestone:";
i++;
ws.Cells[i, 1] = "Alert:";
i++;
ws.Cells[i, 1] = "Region:";
i++;
int j = 1;
ws.Cells[j, 2] = programTxt.Text;
j++;
ws.Cells[j, 2] = dateTxt.Text;
j++;
ws.Cells[j, 2] = vinTxt.Text;
j++;
ws.Cells[j, 2] = dandrTxt.Text;
j++;
ws.Cells[j, 2] = cdsidTxt.Text;
j++;
ws.Cells[j, 2] = milestoneTxt.Text;
j++;
ws.Cells[j, 2] = alertTxt.Text;
j++;
if (EU.IsChecked == true)
{
ws.Cells[j, 2] = EU.Content;
j++;
}
if (Canada.IsChecked == true)
{
ws.Cells[j, 2] = Canada.Content;
j++;
}
if (USA.IsChecked == true)
{
ws.Cells[j, 2] = USA.Content;
j++;
}
wb.SaveAs(@"C:Users\Public\Documents\Report.xls");
wb.Close();
}
EDIT, FIXED
So, we fixed it!
I don't really know why but apparently the file path was the problem. I changed it to WB = _Excel.Workbooks.Open("C:\\Users\\Public\\Documents\\Report.xls");
and now it works !