【发布时间】:2011-12-19 20:24:44
【问题描述】:
我希望从 VBA 中使用一个简单的 C# 函数。为了这个问题,假设 C# 中的例程如下所示:
using System;
namespace MyTestLibrary
{
public static class Class1
{
static string Shout(string message)
{
return message + "!";
}
}
}
我希望能够像这样在 VBA 端使用它。这会导致错误(“找不到 DLL 入口点“Shout”):
Public Declare Function Yell Lib "C:\blah\MyTestLibrary.dll" Alias "Shout" (phrase As String) As String
Sub test()
MsgBox Yell("Hello World")
End Sub
进一步的研究显示了一种解决方法(我已经让这个实现工作):
Sub test()
Dim y As MyTestLibrary.Class1
Set y = New MyTestLibrary.Class1
MsgBox y.Shout("Hello World")
End Sub
但是,这不符合我的要求,因为我无法将我的库与我的工作簿一起部署,因为我必须手动添加参考。对于个人使用来说没什么大不了的,但是当您必须将其部署给非技术导向的最终用户时会很头疼,因为必须手动引用每个 dll。
TLDR如何在 VBA 中使用 C# 制作的静态函数?
【问题讨论】:
-
你不应该让你的类也静态吗?
标签: c# vba dll com-interop