2

Of course the Embed interop types feature is a great thing, but I can't get it work with me even in simple scenario, so please advice this is the error I get when I run my project on machine which didn't have powerPoint installed on it:

enter image description here

My code is very simple I just create object from powerPoint, create presentation and slid write something in it.

The libraries I embed is Office and Microsoft.Office.Interop.PowerPoint

converting build configuration to x68 didn't solve it,

I am building windows application and putting the code in button click as follows:

 private void button1_Click(object sender, EventArgs e)
 {
     var pp = new powerpoint.Application();
     var oPres=pp.Presentations;
     pp.Visible = Office.MsoTriState.msoTrue;
     powerpoint.Presentation  oPre= oPres.Add(Office.MsoTriState.msoTrue);

     powerpoint.Slides oSlides = oPre.Slides;
     powerpoint.Slide oSlide = oSlides.Add(1, powerpoint.PpSlideLayout.ppLayoutText);
     powerpoint.Shapes oShapes = oSlide.Shapes;
     powerpoint.Shape oShape = oShapes[1];
     powerpoint.TextFrame oTxtFrame = oShape.TextFrame;
     powerpoint.TextRange oTxtRange = oTxtFrame.TextRange;
     oTxtRange.Text = "All-In-One Code Framework";

     string fileName = Path.GetDirectoryName(
         Assembly.GetExecutingAssembly().Location) + "\\Sample1.pptx";
     oPre.SaveAs(fileName,
         powerpoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
         Office.MsoTriState.msoTriStateMixed);
     oPre.Close();

     pp.Quit();
     pp = null;
 }

in the top I added

 using powerpoint = Microsoft.Office.Interop.PowerPoint; 
 using Office = Microsoft.Office.Core; 
 using System.IO; 
 using System.Reflection;

Note: the program work fine at my end where I have office 2013 installed, but this error show up at my client pc

Konamiman
  • 49,681
  • 17
  • 108
  • 138
amr osama
  • 1,129
  • 2
  • 18
  • 34
  • possible duplicate of [HRESULT: 0x80040154 (REGDB\_E\_CLASSNOTREG))](http://stackoverflow.com/questions/3003719/hresult-0x80040154-regdb-e-classnotreg) and [How to solve COM Exception Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))?](http://stackoverflow.com/questions/1496214/how-to-solve-com-exception-class-not-registered-exception-from-hresult-0x80040) – cbr Jun 21 '15 at 13:07
  • in this scenario do I have to use Regsvr32.exe at client end?? – amr osama Jun 21 '15 at 14:29
  • "the program work fine at my end where I have office 2013 installed, but this error show up at my client pc" . Well, it's very simple you **must** have Powerpoint installed on the machine where this code runs (with the same bitness as your program: 32-bit vs 64-bit). There's no way around this. – Simon Mourier Jun 25 '15 at 09:29
  • Why is there a bounty on this question, it has been answered. Offering a bounty isnt going to make your software not need powerpoint installed/!! If you have another question (such as what does EmbedInterop do, then ask it as a seperate question. – Toby Allen Jun 25 '15 at 12:50

3 Answers3

9

As already mentioned, the problem is that there is that Powerpoint is not installed on the client machine.

The powerpoint object is implemented in a COM type library and installed and registered when Powerpoint is installed. COM and .NET are totally different technologies. To use COM types from your .NET application, you don't use COM type directly but a special interop .NET assembly. This assembly doesn't contain any PPT functionality, it's just a wrapper that servers as a bridge between your .NET application and a COM type. An interop assembly does all the hard work for you and defines .NET types (from COM types) that you can use as other .NET classes, for example powerpoint.Application.

The interop assembly is just a normal .NET assembly. You can reference it as other .NET references (Embed Interop Types = false). In this case you need to distribute the interop DLL with your application. If you set Embed Interop Types = true, then the interop assembly is compiled and embedded directly to your application assembly. Moreover, only types and functions that are really used are embedded. So using this option has the advantage of optimized and single assembly.

But still, even when embedded, the interop info is just a wrapper over real COM type that must be installed on client machine. If it's not, you get the error. See more details at https://msdn.microsoft.com/en-us/library/xwzy44e4.aspx

Your option is to force the client to install PPT or avoid Microsoft.Office.Interop.PowerPoint and use some third-party PPT library that you can distribute with your application.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Peter Macej
  • 4,831
  • 22
  • 49
2

"Embed interop types" does not mean "embed the Office program". You got this error for a very simple reason, Powerpoint is not installed on that machine.

I supposed that the term "interop types" could use an explanation. You automate an Office program by using COM, an interop technology that preceded .NET and has many similarities to .NET. It also has the notion of metadata, just like a .NET assembly has, the type information that a compiler uses when you add a reference to a .NET assembly. That metadata is called a "type library" in COM. The type library for Powerpoint is C:\Program Files (x86)\Microsoft Office\Office15\MSPPT.OLB for the Office 2013 version.

Type libraries are not exactly perfect, they suffer from ambiguity problems that don't matter much in an application written in C++ but do matter in .NET. So the .NET designers decided to not use a type library directly. And specified the Tlbimp.exe utility, it converts the library content to a .NET assembly. Directly usable by the CLR and managed language compilers without having to deal with the headache of interpreting the type library content.

By convention, the .NET assembly generated by Tlbimp.exe is called an "interop library". It is a one-to-one match with the type library. Most importantly in the context of this question, it only contains declarations, it does not contain executable code. The code remains in the COM component, written in a non-.NET language. Like the Office programs, written in C++.

The interop assembly is required at compile-time, the compiler uses it to know what types are implemented by the COM component so it can properly type-check the code you write and tell you when you get it wrong. It is also required at runtime, it is loaded by the CLR as soon as you use one of the COM types.

Having to deploy the interop assembly to the machine that executes your program is a burden. Not in the least because you often need a "Primary Interop Assembly", yet another mystical term that relates to solving the .NET type identity problem. A detail I'll skip here. The PIAs for Office are very large. It is often very murky exactly who is responsible for ensuring the PIAs are deployed on the machine. With programs failing when nobody takes care of it, a very common mishap.

A burden that Microsoft solved in .NET 4, two COM interface types are considered identical when they have the same [Guid], even when they came from a different assemblies. Which permitted a trick, the compiler can now copy the type declarations from the interop assembly into the output assembly. Only the ones you actually use. Solving both the deployment problem and the bulky interop library problem. You no longer need a PIA and don't have to deploy the interop assembly anymore. This is what "Embed interop types" means.

Long story short, metadata is not enough to execute code. You really do have to have the Office program installed on the target machine. And get this exact exception when it is not.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Funny how the most important information in an error message can be buried so deep its very difficult to see.

Class not Registered

This is your error. Basically the object you are trying to create does not exist or has not bee properly installed on the machine you are running your software on. Is powerpoint definitely installed?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
  • on my development machine, yes office 2013, but on client machine no office installed at all, I think this is all about it, – amr osama Jun 21 '15 at 13:55
  • @amrosama Of course you can't use `Microsoft.Office.Interop` then if there's no Office installed! You'll need to use an external library. – cbr Jun 21 '15 at 14:36
  • @cubrr then what is the meaning of Embed Interop Types = true in reference properties – amr osama Jun 21 '15 at 16:19