Cannot be embedded. Use the applicable interface instead

In your Project, expand the "References", find the Microsoft Office Interop reference. Right click it and select properties, and change "Embed Interop Types" to false.


As explained in a MSDN blog post, instead of disabling "Embed Interop Types" you can also change

xlApp = new Excel.ApplicationClass();

into

xlApp = new Excel.Application();

Although Excel.Application is an interface, we can instantiate it because it is decorated with a CoClass attribute, as explained in this other SO answer: https://stackoverflow.com/a/11039870/501196

Using this approach (Embed Interop Types = true) has the advantage that you will need to deploy less files with your project, and the embedded types will only contain the methods and types that your application is actually using. When you use external interop assemblies, you are importing there all the types and methods exposed by the referenced library.

Tags:

C#