-
I need to create a COM server that can raise events in VBA (using WithEvents) so this requires early binding and adding reference to VBA Tools/References. Essentially I followed the ComServer example but have tried to adapt it to The project works fine late bound using
Does not generate the .tlb file and gives the following error on build (the path to TlbExp.exe is correct on my machine).
I've tried to simplify things by starting with the sample ComServer project but still run into problems with no .tlb file being generated. The only line I've changed in the project is removing
In VBA I get Is this a bug on net8.0 / net6.0 or am I missing something obvious? I can post more if required. |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
I'm sorry the samples and documentation for this are such a mess. Can you try to make the project like this, with <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<PropertyGroup>
<ExcelAddInTlbExp>C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\TlbExp.exe</ExcelAddInTlbExp>
<ExcelAddInTlbCreate>false</ExcelAddInTlbCreate>
<ExcelAddInComServer>true</ExcelAddInComServer>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExcelDna.AddIn" Version="1.8.0" />
<PackageReference Include="dSPACE.Runtime.InteropServices.BuildTasks" Version="1.11.0" />
</ItemGroup>
</Project> |
Beta Was this translation helpful? Give feedback.
-
Excellent, it now works after adding DsCom. Thank you. On a minor note in case anyone else stumbles on this; adding the nuget packages in following order works fine
but adding nuget packages in following order did not
raising following errors. It might be due to requiring VS restart though
|
Beta Was this translation helpful? Give feedback.
-
You don't need to add the package reference for |
Beta Was this translation helpful? Give feedback.
-
Thanks. The .tlb is now being generated and I can add the reference but I still have problems getting the basic COM server working. Calling a method in the class library gives a VBA error
I'm just using the sample ComServer project at the moment with no changes. If I remove the following line then the class library will then call
Any idea what is causing this? |
Beta Was this translation helpful? Give feedback.
-
I had this code using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TestComServer18
{
// Step 1: Defines an event sink interface (MessageEvents) to be implemented by the COM sink.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface MessageEvents
{
void NewMessage(string s);
}
// Step 2: Defines a custom interface for the MessageHandler class.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMessageHandler
{
void FireNewMessageEvent(string s);
}
// Step 3: Connects the event sink interface to a class by passing the namespace and event sink interface
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)] // Avoid using AutoDual
[ComSourceInterfaces(typeof(MessageEvents))]
public class MessageHandler : IMessageHandler
{
public delegate void NewMessageDelegate(string s);
public event NewMessageDelegate NewMessage;
public MessageHandler() { }
public void FireNewMessageEvent(string s)
{
Debug.Print($"New Message {s}");
if (NewMessage != null)
{
Debug.Print($"Invoke {s}");
NewMessage.Invoke(s);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
When I had the AutoDual, DsCom failed at build time though. |
Beta Was this translation helpful? Give feedback.
-
Sorry, still can't get it working. Step2 was not in original sample project so have added. Also added
The VBA project compiles after adding new tlb file but now get 'Class not registered' error. Sorry for taking up so much of your time. If I can get a simple working ComServer project I'll be okay afterwards. |
Beta Was this translation helpful? Give feedback.
-
Be careful with VBA projects that might cache the library. Then further changes are not reflected. You can try You need to build and load the add-in, then open the TestWorkbook.xlsm, Alt+F11, then run the |
Beta Was this translation helpful? Give feedback.
-
TestComServer18 works perfectly, although not quite sure what difference is between earlier examples. Is there a way to ensure VBA clears cached references rather than changing project name or rolling back to an earlier Windows image? |
Beta Was this translation helpful? Give feedback.
I'm sorry the samples and documentation for this are such a mess.
Can you try to make the project like this, with
<ExcelAddInTlbCreate>false</ExcelAddInTlbCreate>
or removed, and adding theDsCom
package reference explicitly (dSPACE.Runtime.InteropServices.BuildTasks
)?