Friday 26 August 2016

Accessing Microsoft Excel without using Interop

In a recent project, the server we deployed to did not have Microsoft Office installed, it only had Microsoft Excel loaded for some reason.

We could not use the C# Microsoft Interop Services to access Excel, and it came up with the error "Could not load file or assembly 'Microsoft.Office.Interop.Excel', or one of its dependencies. The system cannot find the file specified."


Could not load Interop

After searching and not finding much about accessing Excel without using Interop, I stumbled upon an old post from decades ago about using Visual Basic and its CreateObject, GetType, InvokeMember and CallByName functions in the Interaction interface.

Such a godsend, after a few tests I was then able to access Excel through its COM component.


You can check Workbook member properties here: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook_members.aspx


Here is some code to show the process to access the Excel COM Component, it may need adjusting for your own project.

using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Reflection;
using Microsoft.Office.Interop;

PrintDialog PrinterForm = new PrintDialog( );
string FileNamePath = "C:\\ExcelFile.xlsx";

// Create COM object to Excel Application.
Object XLApp = Interaction.CreateObject("Excel.Application");

// Get COM object Excel Workbooks interface.
XLApp.GetType().InvokeMember(
"Workbooks", BindingFlags.GetProperty | BindingFlags.Public, 
null, XLApp, null);

// Test getting Workbooks interface from XLApp COM Object.
Object XLWorkbooks = Interaction.CallByName(
XLApp, "Workbooks", Microsoft.VisualBasic.CallType.Get, null);

// Call Open method on Workbooks interface.
// Pass it the filename parameter.
// Excel COM object should then issue command to Excel.
// Excel to open the filename (xlsx).
Interaction.CallByName(XLWorkbooks, "Open", 
Microsoft.VisualBasic.CallType.Method, FileNamePath);

// Debug (Workbooks object).
// string WorkbooksString = XLWorkbooks.ToString();
// MessageBox.Show("WorkbooksString = [" + WorkbooksString + "]", "Debug");
// Show print form to user.
if (PrinterForm.ShowDialog() == DialogResult.OK)
{
// Cast XLWorkbooks to Workbooks collection.
Microsoft.Office.Interop.Excel.Workbooks XLWorkbooks2 = (Microsoft.Office.Interop.Excel.Workbooks)XLWorkbooks;

// Iterate Workbooks collection.
foreach (Microsoft.Office.Interop.Excel.Workbook XLWorkbook in XLWorkbooks2)
{
// Debug (XLWorkbook).
// MessageBox.Show("XLWorkbook String = [" + XLWorkbook.ToString() + "]", "Debug");
// Print workbook to user specified printer.
XLWorkbook.PrintOutEx(
Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, printDialog.PrinterSettings.PrinterName);

// Close workbook.
XLWorkbook.Close(false);
}
}

// Close COM Excel.
Interaction.CallByName(
XLApp, "Quit", 
Microsoft.VisualBasic.CallType.Method, null);