VBA到COM Exposed .NET程序集 - 对象不支持此属性或方法

时间:2012-02-09 10:33:40

标签: vb.net vba com

是否可以从COM公开的.NET程序集访问共享属性?

VBA

Dim appExcel As Object
    Dim objAppSingleton As Object
    Set objAppSingleton = CreateObject("Pitchbook.CommonUtils.Application.PitchbookAppSingleton")
    appExcel = objAppSingleton.CurrentPitchbookExcelApp

VB.NET

<ProgId("Pitchbook.CommonUtils.Application.PitchbookAppSingleton")> _
Public Class PitchbookAppSingleton

    Private Shared _currentPitchbookExcelApplication As PitchbookAppExcel
    Private Shared _syncLockExcel As Object = New Object()

   Public Shared ReadOnly Property CurrentPitchbookExcelApp As PitchbookAppExcel
        Get
            If _currentPitchbookExcelApplication Is Nothing Then
                                    SyncLock [_syncLockExcel]
                    If _currentPitchbookExcelApplication Is Nothing Then
                        Dim currPitchbookExcelApplication As New PitchbookAppExcel()
                        _currentPitchbookExcelApplication = currPitchbookExcelApplication
                    End If
                End SyncLock
            End If
            Return _currentPitchbookExcelApplication
        End Get
    End Property

End Class


Public Class PitchbookAppExcel
    Inherits PitchbookApp

    Protected Friend Sub New()
        MyBase.New()                        
    End Sub

End Class

行appExcel = objAppSingleton.CurrentPitchbookExcelApp给出错误: 运行时错误'438': Object不支持此属性或方法

2 个答案:

答案 0 :(得分:1)

您需要在VB中说 Set 来设置对象变量。

Set appExcel = objAppSingleton.CurrentPitchbookExcelApp 

如果您没有说“Set”,那么VBA将查找默认方法,并调用它来获取非对象类型。如果没有定义默认方法(dispid = 0),那么它将因你得到的错误而失败。

答案 1 :(得分:0)

这是一个非常简单的问题,但如果有人不知道答案,那么不能从COM公开的.NET程序集中访问共享属性。至少不是直接...但是,如果您创建实例包装器,则可以访问共享属性。

http://www.xtremevbtalk.com/showthread.php?p=1326018
http://support.microsoft.com/Default.aspx?kbid=817248

在我的情况下,我使用Facade模式创建一个专门供VBA使用的对象,该对象通过实例公开相关的共享实用程序函数。感谢大家的评论。 http://en.wikipedia.org/wiki/Facade_pattern