Note that there are some explanatory texts on larger screens.

plurals
  1. POVSTO in VBA: AddIn.Object returns Nothing (null) sometimes
    primarykey
    data
    text
    <p>Given:</p> <ul> <li>A VSTO Add-In</li> <li>An <code>override object RequestComAddInAutomationService()</code> which returns an instance of a class which is called <code>Facade</code> in my scenario.</li> <li>A VBA macro in Excel 2007 which accesses the <code>AddIn.Object</code> to get the Facade and uses it.</li> <li>A plenty of times where this works perfectly fine.</li> <li><strong>A couple of times where out of the blue, this doesn't seem to work.</strong></li> </ul> <p><strong>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")</strong></p> <p>In this "couple of times" I get</p> <blockquote> <p>Error: Object variable or With block variable not set</p> </blockquote> <p>at the line of code which tries to access a property of <code>Facade</code>. In short I can tell you that the code in <code>RequestComAddInAutomationService()</code> 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 :-)</p> <p><strong>Question: Does anyone have a clue why this can happen? Is it an Excel issue?</strong></p> <hr> <p>Details as promised:</p> <p>MyAddIn.cs:</p> <pre><code>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; } } </code></pre> <p>Facade.cs:</p> <pre><code>[ComVisible(true)] [Guid("1972781C-A71A-48cd-9675-AE47EACE95E8")] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface IFacade { // some methods } [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class Facade : IFacade { private Controller Controller { get; set; } public Facade(Controller controller) { this.Controller = controller; } } </code></pre> <p><code>Facade</code> has some methods but not a single field.</p> <p>Controller.cs:</p> <pre><code>public class Controller { private static Controller instance = null; public static Controller Instance { get { if (instance == null) instance = new Controller(); return instance; } } private Controller() { } } </code></pre> <p><code>Controller</code> 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 <code>null</code>, so the constructor does virtually nothing.</p> <p>The VBA code:</p> <pre><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() </code></pre> <p>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 <code>null</code>, then the statement would simply evaluate to <code>Set oResult = Nothing</code> which is still valid. VBA rather throws this type of error whenever a method is executed on an reference that is <code>Nothing</code>, which is <code>automationObject</code> in my case.</p> <p>On the other hand, if the add-in wasn't there at all, the <code>Application.COMAddIns(...)</code> would raise an <em>index out of bounds</em> error, I've seen that before.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload