66

I am trying to add a photo to a Excel Spread sheet but keep getting the following error?

Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.

ApplicationClass(); is underlined in red in the line of code below:

xlApp = new Excel.ApplicationClass();

Could Some on please tel me how i could fix this?

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;

private void btnWriteSpreedSheet_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass(); //This is where the problem is??????
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            //add some text 
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
            xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";

            xlWorkSheet.Shapes.AddPicture("C:\\csharp-xl-picture.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);

              xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);

            MessageBox.Show ("File created !");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 

    }
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Pomster
  • 14,567
  • 55
  • 128
  • 204

3 Answers3

223

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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Hey thanks that seemed to have removed the read line error :) – Pomster Jul 27 '12 at 13:34
  • 1
    This is an OK solution, but the answer that currently has 21 'up votes' is a better answer imho, ==== changing xlApp = new Excel.ApplictionClass(); to xlApp = new Excel.Application is a better answer (and probably should be marked as the correct solution)... See related answer for more detail. – Danimal111 Oct 01 '15 at 15:00
  • The answer below should really be marked as the correct answer for the reasons given there. – sunefred Jun 14 '16 at 18:41
  • Why is the other answer better? I'm guessing embedded types are important in case the type isn't found on the machine the application is running on. Is that the case? – Kyle Delaney Mar 16 '17 at 16:06
  • Hey Thanks man. It worked for me! – Amol Jadhav Aug 23 '18 at 06:15
  • Wow thanks for this! Compared my old project and it has it set to False. Somehow when I added a new reference with interop it set this flag to True. – coredumper Sep 13 '22 at 19:40
76

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.

yms
  • 10,361
  • 3
  • 38
  • 68
  • 6
    This is the better solution in my opinion. Changing Embed Interop Types to false causes issues if you distribute your solution onto other client machines. Whereas the simple change of syntax in this solution solved the error message and ensured the solution still ran on other client machines. – PJW Nov 10 '14 at 10:05
  • This solution helped me microsoft.office.interop.outlook ver 16 – mend0k Jul 08 '21 at 18:01
  • The first link is broken. But works. – Oliver Jul 27 '21 at 12:13
  • 1
    @Oliver fixed using web.archive.org, thanks – yms Jul 29 '21 at 17:48
-2

Find Microsoft.Office.Interop.Excel Reference and right click and change "Embed Interop Types" state falce if its true.

RDVN
  • 7