[英]How to call a C# function from VBA and Excel
I installed VSTO on Visual Studio 2019 and I started writing a simple script like this:我在 Visual Studio 2019 上安装了 VSTO,然后开始编写一个简单的脚本,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private double ReturnMagicNumber()
{
return 123;
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
I would like to know how to make ReturnMagicNumber()
function available in Excel as an User Function and also how can I call it from a VBA Macro? I would like to know how to make
ReturnMagicNumber()
function available in Excel as an User Function and also how can I call it from a VBA Macro?
Thanks谢谢
VSTO doesn't provide anything for developing user-defined functions for Excel. VSTO 不提供任何用于为 Excel 开发用户定义函数的东西。 You need to create such projects on your own - it can be an automation add-in or XLL one.
您需要自己创建此类项目 - 它可以是自动化插件或 XLL 插件。 Both approaches are well described in the Using C# .NET User Defined Functions (UDF) in Excel article.
这两种方法在 Excel 文章中的使用 C# .NET 用户定义函数 (UDF) 中都有很好的描述。
If your choice is XLL add-ins you may consider using Excel-DNA which simplifies development a lot.如果您选择 XLL 插件,您可以考虑使用Excel-DNA ,它可以大大简化开发。
Ok I got it working without the need of Excel-DNA.好的,我不需要 Excel-DNA 就可以正常工作。
For the solution I read this here and here , and the code is the following:对于我在这里和这里阅读的解决方案,代码如下:
The C# code on VS 2019 is the following: VS 2019上的C#代码如下:
using System;
using System.Data;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace MagicNumberAddIn
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface INumberGetter
{
double ExcelReturnNumber();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class NumberGetter : INumberGetter
{
public double ExcelReturnNumber()
{
return 123.0;
}
}
public partial class MagicNumber
{
private NumberGetter myAddIn;
protected override object RequestComAddInAutomationService()
{
if (myAddIn == null)
myAddIn = new NumberGetter();
return myAddIn;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Then the code on VBA is this:那么VBA上的代码是这样的:
Public Function GetMagicNumber() As Variant
Dim addin As Office.COMAddIn
Dim automationObject As Object
Dim returnNumber As Double
Set addin = Application.COMAddIns("MagicNumberAddIn")
Set automationObject = addin.Object
returnNumber = automationObject.ExcelReturnNumber()
GetMagicNumber = returnNumber
End Function
This works for me because what I want is to be able to use C# Add-ins inside VBA in order to get Multitasking and Asynchronous Functions in VBA.这对我有用,因为我想要的是能够在 VBA 中使用 C# 插件,以便在 VBA 中获得多任务和异步功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.