简体   繁体   English

从excel VBA初始化C#类

[英]initialize a C# class from an excel VBA

I have a class in VBA: 我在VBA中有一个课程:

Option Explicit

Private mo As omMsg.Message

Private Sub Class_Initialize()
 Set mo = New omMsg.Message
End Sub

Private Sub mo_OnMessageSend(ByVal status As omMsg.ProcessStatus, ByVal msg As String)
   ProcessComMessage status, msg
End Sub

the mo_OnMessageSend is not getting called. 没有调用mo_OnMessageSend。

The C# class is as follows: C#类如下:

namespace omMsg
{   
public interface IMessage
{
    void sendMessage(Message.ProcessStatus status, string msg);       
    event Message.MessageHandler OnMessageSend;
}

[ClassInterface(ClassInterfaceType.None)]
public class Message : IMessage
{
    public enum ProcessStatus { Retrieving, Generating, Error, Complete }

    public delegate void MessageHandler(ProcessStatus status, string msg);
    public event MessageHandler OnMessageSend;

    public void sendMessage(ProcessStatus status, string msg)
    {
        if (OnMessageSend != null)
            OnMessageSend(status, msg);
    }
}
}

Please help. 请帮忙。 Am i doing the right way? 我做对了吗?

Thanks. 谢谢。

A Basic Walk Through : 基本演练

We'll start by walking through a very basic example. 我们将通过一个非常基本的示例开始。 We'll get Excel to call a .NET method that takes a string as input (for example “ World”) and returns “Hello” concatenated with that input string (so, for example, “Hello World”). 我们将使Excel调用.NET方法,该方法将字符串作为输入(例如“ World”),并返回与该输入字符串连接的“ Hello”(例如,“ Hello World”)。

-Create a C# Windows class library project in Visual Studio 2005 called 'DotNetLibrary'. -在Visual Studio 2005中创建一个名为“ DotNetLibrary”的C#Windows类库项目。 It doesn't matter which folder this is in for the purposes of this example. 在此示例中,该文件夹位于哪个文件夹中都没有关系。

-To call a method in a class in our library from Excel we need the class to have a default public constructor. -要从Excel中调用库中类的方法,我们需要该类具有默认的公共构造函数。 Obviously the class also needs to contain any methods we want to call. 显然,该类还需要包含我们要调用的任何方法。 For this walk through just copy and paste the following code into our default class file: 对于本演练,只需将以下代码复制并粘贴到我们的默认类文件中:

using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetLibrary
{
    public class DotNetClass
    {
        public string DotNetMethod(string input)
        {
            return "Hello " + input;
        }
    }
}

That's it: if you look at existing articles on the web, or read the MSDN help, you might think you need to use interfaces, or to decorate your class with attributes and GUIDs. 就是这样:如果您查看网络上的现有文章或阅读MSDN帮助,则可能会认为您需要使用接口,或使用属性和GUID装饰类。 However, for a basic interop scenario you don't need to do this. 但是,对于基本互操作方案,您不需要这样做。

-Excel is going to communicate with our library using COM. -Excel将使用COM与我们的图书馆进行交流。 For Excel to use a COM library there need to be appropriate entries in the registry. 为了使Excel使用COM库,注册表中需要有适当的条目。 Visual Studio can generate those entries for us. Visual Studio可以为我们生成这些条目。

To do this bring up the project properties (double-click 'Properties' in Solution Explorer). 为此,调出项目属性(在解决方案资源管理器中双击“属性”)。 Then: i) On the 'Application' tab click the 'Assembly Information…' button. 然后:i)在“应用程序”选项卡上,单击“组装信息...”按钮。 In the resulting dialog check the 'Make assembly COM-visible' checkbox. 在出现的对话框中,选中“使装配COM可见”复选框。 Click 'OK'. 点击“确定”。 ii) On the 'Build' tab check the 'Register for COM interop' checkbox (towards the bottom: you may need to scroll down). ii)在“构建”选项卡上,选中“注册COM互操作”复选框(朝下:您可能需要向下滚动)。

-Build the library. -建立图书馆。

-Now start Excel and open a new blank workbook. -现在启动Excel并打开一个新的空白工作簿。 Open the VBA code editor: i) In Excel 2007 this is a little difficult to find. 打开VBA代码编辑器:i)在Excel 2007中很难找到它。 You have to get the Developer tab visible on the Ribbon if it's not already set up. 如果尚未设置,则必须在功能区上显示“开发人员”选项卡。 To do this click the Microsoft Office Button (top left of the screen), then click Excel Options (at the very bottom). 为此,请单击“ Microsoft Office按钮”(位于屏幕左上方),然后单击“ Excel选项”(位于最底部)。 Check the 'Show Developer tab in the Ribbon' checkbox in the resulting Options dialog. 在出现的“选项”对话框中,选中“在功能区中显示开发人员选项卡”复选框。 Click OK. 单击确定。 This adds 'Developer' to the end of the ribbon menu: click this. 这会在功能区菜单的末尾添加“ Developer”:单击此。 Then click the 'Visual Basic' icon at the left end of the ribbon. 然后单击功能区左端的“ Visual Basic”图标。 ii) In earlier versions of Office (2003, XP, 2000) just go to Tools/Macro/Visual Basic Editor on the menu bar. ii)在早期版本的Office(2003,XP,2000)中,只需转到菜单栏上的“工具/宏/ Visual Basic编辑器”。

-We now need to include a reference to our new library. -现在我们需要包含对新库的引用。 Select 'References' on the Visual Basic Editor's 'Tools' menu. 在Visual Basic编辑器的“工具”菜单上选择“参考”。 If you scroll down in the resulting dialog you should find that 'DotNetLibrary' is in the list. 如果在出现的对话框中向下滚动,则应该在列表中找到“ DotNetLibrary”。 Check the checkbox alongside it and click 'OK'. 选中旁边的复选框,然后单击“确定”。

-Now open the code window for Sheet1 (double click Sheet1 in the Project window). -现在打开Sheet1的代码窗口(在“项目”窗口中双击Sheet1)。 Paste the VBA code below into the code window for Sheet1: 将下面的VBA代码粘贴到Sheet1的代码窗口中:

Private Sub TestDotNetCall() 
Dim testClass As New DotNetClass 
MsgBox testClass.DotNetMethod(“World”) 
End Sub

-Click anywhere in the code you've just pasted in and hit 'F5' to run the code. -在您刚刚粘贴的代码中的任意位置单击,然后单击“ F5”以运行代码。 You should get a 'Hello World' message box. 您应该会收到一个“ Hello World”消息框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM