horgh
horgh

Reputation: 18534

Microsoft.Office.Interop.Excel.ApplicationClass has no constructor defined

I tried to follow How to open an Excel file in C# tutorial, i.e. added a reference on the Com tab to Microsoft Office 14.0 Object Library and tried to compile code:

using Excel = Microsoft.Office.Interop.Excel;
//...
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

xlApp = new Excel.ApplicationClass();//error here
//...

and faced a compile-time error, saying

There is no constructor defined for Microsoft.Office.Interop.Excel.ApplicationClass type.

What am I missing?

Upvotes: 22

Views: 42226

Answers (3)

Mehrad
Mehrad

Reputation: 4203

If you're using C# 4.0 (.NET 4) you can go with much easier syntax

var app = new Application( );

var workbook = app.Workbooks.Open("test.xls");

In regards to var: it makes you job easier cuz C# decides which type to chose and go for. If interested you can read about dynamic and var styles.

Remember that interop prior to C# 4.0 is totally a different phenomenon and how C# used to handle Microsoft objects.

just so you know how different, this is how you should've coded it before C# 4.0 to talk to a Word document.

object fileName = @"WordFile.docx";
object missing = System.Reflection.Missing.Value;
object readOnly = true;
var doc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);

Upvotes: 3

PeterJ
PeterJ

Reputation: 3795

Use the following to open it:

xlApp = CreateObject("Excel.Application");

CreateObject creates and returns a reference to a COM object. Documentation may be found here:

http://msdn.microsoft.com/en-us/library/7t9k08y5%28v=vs.71%29.aspx

Upvotes: 3

Allan Spreys
Allan Spreys

Reputation: 5697

Try this:

Excel._Application xlApp = new Excel.Application();

Upvotes: 39

Related Questions