我有沒有問題寫一個.NET的WinForms可執行文件,其中露出一些類到COM,並在Excel中使用VBA他們經過是這樣的:如何將運行的.NET應用程序對象暴露給COM對於VBA.GetObject? (不重複)
Set myVbaObj = VBA.CreateObject("MyNameSpace.MyClassName")
myVbaObj.SayHi "I hope somebody will answer this question."
凡我.NET類已經取得了COM正是如此可見:
using System.Runtime.InteropServices;
namespace MyNameSpace
{
[Guid("GUID created by Visual Studio->Tools->Create GUID")]
public interface IMyClassName
{
void SayHi(string Msg);
}
[ComVisible(true)]
[Guid("Other GUID created by Visual Studio->Tools->Create GUID")]
[ProgId("MyNameSpace.MyClassName")]
[ClassInterface(ClassInterfaceType.None)]
public class MyClassName : IMyClassName
{
public MyClassName()
{
RegistrationServices rs;
try
{
// Class constructor registers itself for COM
rs = new RegistrationServices();
if (!rs.RegisterAssembly(Assembly.GetExecutingAssembly(),
AssemblyRegistrationFlags.SetCodeBase))
{
throw new Exception
("Error registering MyClassName COM component.");
}
}
catch (Exception ex)
{
throw new Exception(string.Format(
"Error in {0} constructor:\r\n{1}",
this.GetType().Name, ex.Message));
}
// End of MyClassName constructor
}
[ComVisible(true)]
public void SayHi(string Msg)
{
try
{
MessageBox.Show(string.Format(
"Hi from Excel: '{0}'.", Msg));
}
catch (Exception ex)
{
throw new Exception(string.Format(
"Error in {0}.SayHi:\r\n{1}",
this.GetType().Name, ex.Message));
}
// End of SayHi()
}
}
}
不過,我想成爲像Excel和暴露的.exe文件,所以我可以參考和使用運行我的.NET應用程序的實例進行交互。在Excel VBA中,出現這種情況通過「GetObject的」:
Set myVbaObj = VBA.GetObject(, "MyNameSpace.Application")
但我難倒得到類似的東西實際上工作方式下面將在Excel VBA(立即窗口)工作:
Set xlApp = VBA.GetObject(, "Excel.Application")
print xlApp.caption
編輯:放入我最初省略的界面定義。