我为此苦了好几个小时,但我找不到我做错了什么。
我创建了一个新的C#dll项目,这是它包含的唯一类的内容:
using System;
using System.Runtime.InteropServices;
namespace PolygonSl {
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Config {
[ComVisible(true)]
public string GetCompany() {
return "POL";
}
}
}
我基本上从其中删除了所有内容以使其正常运行,唯一的参考是System
。
我检查了Make assembly COM-Visible
上的Assembly Information
标志,并且我的项目已签名(代码库所需的接缝)。
它可以很好地编译,之后,我调用RegAsm.exe
,并给它命名为dll,并添加了/codebase
和/tlb
,命令成功。
当我进入VBA项目时,可以将新的tlb文件添加到引用中,可以正常工作。之后,我可以在代码中使用它,自动完成功能可以正常工作,并且可以编译而不会出现错误。
然后,当我执行时,我得到了:
Run-time error '430':
Class does not support Automation or does not support expected interface
这是我在VBA中的代码示例:
Private Sub Button1_Click()
'With CreateObject("PolygonSl.Config")
With New PolygonSl.Config
MessBox .GetCompany, MB_OK, "Test"
End With
End Sub
我尝试了后期绑定,但是我的代码可以正常运行,但是我希望能够使用自动完成功能。
有人对我可以如何使其起作用提出建议?
编辑(添加有关我的环境的一些详细信息)
答案 0 :(得分:1)
我认为您需要定义一个接口才能看到getcompany。
using System;
using System.Runtime.InteropServices;
namespace PolygonSl
{
[Guid("6DC1808F-81BA-4DE0-9F7C-42EA11621B7E")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IConfig
{
string GetCompany();
}
[Guid("434C844C-9FA2-4EC6-AB75-45D3013D75BE")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(ClassInterfaceType.None)]
public class Config : IConfig
{
public string GetCompany()
{
return "POL";
}
}
}
通过将光标放在类定义中并使用Edit.Refactor.ExtractInterface,可以自动生成接口。
我必须承认我在这里的能力处于绝对优势,以上内容是根据我在其他地方看到的示例综合而成的。
编辑
以下测试代码在我的PC上正常工作
Option Explicit
Sub polygontest()
Dim my_polygon As SOPolygon.Config
Set my_polygon = New SOPolygon.Config
Debug.Print my_polygon.GetCompany
End Sub
SOPolygon是项目名称。