chiccodoro chiccodoro - 1 month ago 5x
C# Question

VSTO in VBA: AddIn.Object returns Nothing (null) sometimes


  • A VSTO Add-In

  • An
    override object RequestComAddInAutomationService()
    which returns an instance of a class which is called
    in my scenario.

  • A VBA macro in Excel 2007 which accesses the
    to get the Facade and uses it.

  • A plenty of times where this works perfectly fine.

  • A couple of times where out of the blue, this doesn't seem to work.

Update: Turns out that it's a particular user that has the problem. She has it all the time, others never have it (? never say "never")

In this "couple of times" I get

Error: Object variable or With block variable not set

at the line of code which tries to access a property of
. In short I can tell you that the code in
doesn't have any error-prone magic in it, and the VBA code to access the add-in has been taken from the web and looks fine, too. The longer version is yet to come, for those who'll take the time to read it :-)

Question: Does anyone have a clue why this can happen? Is it an Excel issue?

Details as promised:


public partial class MyAddIn
public Facade Facade { get; private set; }

protected override object RequestComAddInAutomationService()
if (this.Facade == null)
this.Facade = new Facade(Controller.Instance);

return this.Facade;


public interface IFacade
// some methods

public class Facade : IFacade
private Controller Controller { get; set; }

public Facade(Controller controller)
this.Controller = controller;

has some methods but not a single field.


public class Controller
private static Controller instance = null;
public static Controller Instance
if (instance == null) instance = new Controller();
return instance;

private Controller() { }

has some private fields. Since the fields assignments are executed on creation, I reviewed them. Most of them are not initialized at all, or they are set to
, so the constructor does virtually nothing.

The VBA code:

Dim addin As Office.COMAddIn
Dim automationObject As Object

Set addin = Application.COMAddIns("My AddIn")
Set automationObject = addin.Object

Dim oResult As Object
Set oResult = automationObject.SomeMethodThatReturnsAnObject()

The last line is where the error happens. Although the method called returns an object, I am pretty sure that it cannot be the source of the error: If the reference returned was
, then the statement would simply evaluate to
Set oResult = Nothing
which is still valid. VBA rather throws this type of error whenever a method is executed on an reference that is
, which is
in my case.

On the other hand, if the add-in wasn't there at all, the
would raise an index out of bounds error, I've seen that before.


Turned out that Excel disabled the COM add-in. This is known to sometimes happen silently, without Excel complaining about anything.

So, since the add-in was registered with excel, the following line succeeded:

Set addin = Application.COMAddIns("My AddIn")

But since it was disabled, the object was not created and

Set automationObject = addin.Object

resulted in Nothing.