views:

79

answers:

1

This is a very common problem when Excel Worksheet or Chart is embedded into Word or Powerpoint. I am seeing this problem in both Word and Powerpoint and the reason it seems is the COM addin attached to Excel. The COM addin is written in C# (.NET). See the attached images for error dialogs.

I debugged the addin and found a very strange behavior. The OnConnection(...), OnDisConnection(...) etc methods in the COM addin works fine until I add an event handler to the code. i.e. handle the Worksheet_SheetChange, SelectionChange or any similar event available in Excel. As soon as I add even a single event handler (though my code has several), Word and Powerpoint start complaining and do not Activate the embedded object.

On some of the posts on the internet, people have been asked to remove the anti-virus addins for office (none in my case) so this makes me believe that the problem is somewhat related to COM addins which are loaded when the host app activates the object.

Does anyone have any idea of whats happening here?PowerPoint Error

Word Error

UPDATED 21-JUNE-2010


Found out that both Events and changes to ComAddIns collection creates problems when the embedded object is activated. I have now used the Excel::Application::UserControl property to check whether Excel is in embedded state and then skip any OnConnection(...) and OnDisconnection(...) code.

One solution to Events problem can be to move all the application level events to VBA code and call into .NET. Thereby removing all event handlers from .NET code.

There may be even more scenarios where an embedded object might fail to initialize so I choose to disable the COM addin i.e. skip the code in OnConnection(...) and OnDisconnection(...) methods altogether.

A: 

Here is what I did to get rid of this annoying problem:

  1. Remove all event handlers in the .NET code and rely on the VBA Application Events i.e. Instead of handling Excel::Application::Worksheet_Activate(...) event, handle them in a VBA module and call into the addin when an event is received. The .NET event handlers seems to corrupt the state somehow.

  2. Disable any code that interacts with the addin in OnConnection(...) using the flag in custom param. See the following link for details on the flag: COM AddIns in Detail

I mentioned about the Excel::Application::UserControl property but it is not reliable in some cases. We need to consider the following cases and the custom array param is OnConnection(...) is the most reliable:

  1. Excel started as independent application
  2. Excel started as embedded app in some other application
  3. Excel started through automation e.g. CreateObject(...)

I preferred disabling the addin totally when Excel is in embedded mode. It depends what code you are executing in your OnConnection(...) and how other applications respond to it when executed. Point #1 has to be implemented to solve this issue.

If anybody has a better solution to this, please let me know :)

A9S6