VSTO在VBA:AddIn.Object返回Nothing(空)有时VBA、VSTO、AddIn、Nothing

2023-09-07 10:09:43 作者:爱是回不去的旅行

由于:

系统VSTO加载项 的覆盖对象RequestComAddInAutomationService()返回一个被称为外观在我的场景类的一个实例。 在VBA宏在Excel 2007中它访问 AddIn.Object 获得外观和使用它。 系统很多次,在那里这工作完全正常。 有几次在哪里出了蓝色,这似乎并没有工作。 A VSTO Add-In An override object RequestComAddInAutomationService() which returns an instance of a class which is called Facade in my scenario. A VBA macro in Excel 2007 which accesses the AddIn.Object 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.

更新:原来,这是有问题的特定用户。她拥有它的时候,别人也永远不可能拥有一切(?从来不说从来没有)

在此几次我知道

错误:对象变量或With没有设置块变量

Error: Object variable or With block variable not set

在code中的线,试图访问外观的属性。总之,我可以告诉你,code。在 RequestComAddInAutomationService()没有任何出错的魔法在里面,和VBA code访问外接已经采取了从网页看起来不错,太。在更长的版本尚未到来,对于那些谁就会花时间来阅读: - )

at the line of code which tries to access a property of Facade. In short I can tell you that the code in RequestComAddInAutomationService() 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 :-)

问:有没有人有一个线索,为什么会发生这种情况?它是一个Excel的问题?

详细的承诺:

MyAddIn.cs:

MyAddIn.cs:

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;
    }
}

Facade.cs:

Facade.cs:

[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;
    }
}

外观有一些方法,但不是一个单一的领域。

Facade has some methods but not a single field.

Controller.cs:

Controller.cs:

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

    private Controller() { }
}

控制器有一些私人领域。由于字段赋值在创建时会执行,我回顾了他们。他们中的大多数都不会在所有的初始化,或者它们设置为,所以构造函数做几乎没有。

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 null, so the constructor does virtually nothing.

在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()

最后一行就是错误发生。虽然叫方法返回一个对象,我是pretty的肯定,它不能是错误的根源:如果参考返回信息,那么该语句将简单地计算结果为设置oResult =无这仍然是有效的。 VBA而引发此类型的错误,每当一个方法上的参考是没有,这是 automationObject 在执行我的案例。

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 null, 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 Nothing, which is automationObject in my case.

在另一方面,如果该加载项是根本不存在的 Application.COMAddIns(...)将引发的索引列界的错误,我已经看到过。

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

推荐答案

原来是Excel中禁用的COM加载项。这是众所周知的,有时静静地发生,没有Excel的抱怨什么。

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

那么,既然该加载项注册到Excel中,以下行成功了:

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

导致没有