Reputation: 4640
I have a class in 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.
The C# class is as follows:
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.
Upvotes: 0
Views: 589
Reputation: 65692
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”).
-Create a C# Windows class library project in Visual Studio 2005 called ‘DotNetLibrary’. 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. 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. However, for a basic interop scenario you don’t need to do this.
-Excel is going to communicate with our library using COM. For Excel to use a COM library there need to be appropriate entries in the registry. Visual Studio can generate those entries for us.
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. In the resulting dialog check the ‘Make assembly COM-visible’ checkbox. Click ‘OK’. ii) On the ‘Build’ tab check the ‘Register for COM interop’ checkbox (towards the bottom: you may need to scroll down).
-Build the library.
-Now start Excel and open a new blank workbook. Open the VBA code editor: i) In Excel 2007 this is a little difficult to find. 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). 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. Then click the ‘Visual Basic’ icon at the left end of the ribbon. ii) In earlier versions of Office (2003, XP, 2000) just go to Tools/Macro/Visual Basic Editor on the menu bar.
-We now need to include a reference to our new library. Select ‘References’ on the Visual Basic Editor’s ‘Tools’ menu. If you scroll down in the resulting dialog you should find that ‘DotNetLibrary’ is in the list. Check the checkbox alongside it and click ‘OK’.
-Now open the code window for Sheet1 (double click Sheet1 in the Project window). Paste the VBA code below into the code window for 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. You should get a ‘Hello World’ message box.
Upvotes: 1