Home » excel » c# – Make assembly COM-Visible breaks build for Excel VSTO addin

c# – Make assembly COM-Visible breaks build for Excel VSTO addin

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on a C# VSTO Excel Addin, which will be called from some excel VBA. Therefore I must:

  • Register for COM interop, and
  • Make assembly COM-Visible

The register does not break anything, but making the assembly COM visible produces the following two errors:

Error       The assembly "C:\Users\...\ExcelInterop.dll" could not be converted to a type library. Type library exporter encountered an error while processing 'ExcelInterop.ThisAddIn, ExcelInterop'. Error: Error loading type library/DLL.   ExcelInterop            
Error       The assembly 'Microsoft.Office.Tools.Common.v4.0.Utilities, Version=10.0.0.0, Culture=neutral, PublicKeyToken=...' is not registered for COM Interop. Please register it with regasm.exe /tlb.  ExcelInterop            

It works fine if I create a normal class library project, so I am guessing it has to do with ThisAddin.cs, which links the C# to excel worksheets. Note, I have not changed ThisAddin.cs at all from the boilerplate which is generated.

What am I trying to do?

From my VSTO Excel Addin, I must import/export some excel data and send it to a business API (which cannot be accessed from the VBA Excel addin). However, the VBA provides the UI for the client (among other things), which is why I cannot just rewrite the entire VBA addin in C#.

How to&Answers:

Reading the following SO post helped me better understand what COM-Visible meant…

https://stackoverflow.com/questions/3699767/register-for-com-interop-vs-make-assembly-com-visible

I simply needed to mark the class I needed to use in VBA as seen below, instead of making the entire assembly COM-Visible:

[GuidAttribute("54c23560-db8d-4ec9-a19f-f920ba0558c0"), ComVisible(true)]
public class Manager
{
...
}

I generated the Guid from https://www.guidgenerator.com/online-guid-generator.aspx.