Brief details:
Running Office 360 with Excel 64bit installed. Application is built on .NET 4.6 and CPU type is set to x64
The code that's not working - line marked where it fails
public bool CreateInvoice()
{
try
{
//Write file
var setting = new Classes.Settings();
string WritePath = setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".xlsx";
if (!System.IO.Directory.Exists(setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID))
System.IO.Directory.CreateDirectory(setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID);
if (System.IO.File.Exists(WritePath))
System.IO.File.Delete(WritePath);
System.IO.File.WriteAllBytes(WritePath, AccountManagement.Properties.Resources.Invoice);
//Prepare Excel
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks xlWorkBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlExportSheet = null;
Classes.GetExcelProcessID getExcelProcessID = new GetExcelProcessID();
xlWorkBooks = xlApp.Workbooks; //***********FAILS HERE*************
xlWorkBook = xlWorkBooks.Add(WritePath);
xlSheets = xlApp.Worksheets;
xlExportSheet = (Microsoft.Office.Interop.Excel.Worksheet)(xlSheets[1]);
var cells = xlExportSheet.Cells;
var Process = Classes.GetExcelProcessID.ExcelProcesID(xlApp);
try
{
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
//This is where I make my edits.
xlWorkBook.SaveAs(WritePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".pdf");
InvoiceSavePath = setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".pdf";
InvoiceDetails.InvoiceSavePath = InvoiceSavePath;
}
catch (Exception ex)
{
return false;
}
finally
{
//Ensue all COM objects are closed and realesed or EXCEl will remain open.
xlWorkBook.Close(0, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
xlWorkBooks.Close();
//xlApp.Application.Quit();
xlApp.Quit();
while (Marshal.FinalReleaseComObject(xlApp) != 0) { }
while (Marshal.FinalReleaseComObject(xlWorkBooks) != 0) { }
while (Marshal.FinalReleaseComObject(xlWorkBook) != 0) { }
while (Marshal.FinalReleaseComObject(xlSheets) != 0) { }
while (Marshal.FinalReleaseComObject(xlExportSheet) != 0) { }
while (Marshal.FinalReleaseComObject(cells) != 0) { }
xlApp = null;
xlWorkBooks = null;
xlWorkBook = null;
xlSheets = null;
xlExportSheet = null;
cells = null;
try
{
Process.Kill();
}
catch
{
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
This is the exception I keep getting.
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'
I initially thought the issue was about my application possibly still being built at x86 but I have a interop function with Word that runs perfectly fine - and that's 64bit.
I've tried google but all the fixes are to do with 64bit office and a 32bit application. Any tips?