プロジェクトのプロパティ > ビルド > COM相互運用機能の登録をチェックオン
プロジェクトのプロパティ > アプリケーション > アセンブリ情報 > アセンブリをCOM参照可能にする をチェックオン
using System;
using System.Runtime.InteropServices;
namespace sampleClass
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("A887F287-084D-44A4-BE0B-859CB6DB1631")]
public interface IFoo
{
string Hello(string name);
}
[ClassInterface(ClassInterfaceType.None)]
//これがないと、引数ありのHelloメソッドがVBAから実行できない
[ProgId("ClassLibrary1.Foo")]
[Guid("EAF36BB4-23A9-476D-9557-E81AFA2932B8")]
public class Foo:IFoo
{
public String Hello(string name)
{
return "Hello " + name ;
}
}
}
エクセルでVBAエディタを起動。プロジェクト右クリック > 挿入 > 標準モジュール
ツール > 参照設定 > sampleClassをチェックオン
表示 > イミディエイト ウィンドウ 標準モジュールに以下を記述。
Sub test()
Dim foo As New sampleClass.foo
Debug.Print foo.Hello("Bar")
End Sub
0 件のコメント :
コメントを投稿