Excel Interop

COM Interop is a term that is used to describe backwards compatibility between COM and .NET.
Components written with COM technology are able to use assemblies written with .NET technology. And assemblies written with .NET technology are able to use COM components.
This is all possible without changing the actual components.
If you want .NET code to communicate directly with a COM object (for example Excel) then you have to use a proxy or wrapper class.
Interoperating between .NET and COM is made possible thanks to two wrappers.
https://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-.net-com-assembly/


Wrapper - COM Callable Wrapper

Allows a COM component to access a .NET component.
For example VBA calling C#.



Wrapper - Runtime Callable Wrapper (RCW)

Allows a .NET component to access a COM component.
For example C# calling VBA.
There are a number of ways to create this wrapper class.
One way is to use the command line program TLBIMP.exe to generate it for you.


Visual Studio will do this for you.
Select (Project > Add Reference)(COM tab and select the COM component you want to access).
Visual Studio will generate the necessary wrapper class for you and add the generated DLL as a reference.


The Primary Interop Assemblies are examples of Runtime Callable Wrappers that Microsoft provides for you.
Because Microsoft has provided these for you, so you do not have to manually generate them.
These assemblies are automatically signed with a strong name and added to the Global Assembly Cache.
You can reference a PIA by selecting (Project > Add Reference)(.NET tab)


Exception Handling

Any exceptions that are generated through the RCW are automatically converted into equivalent .NET exception objects.
Any error that generates an unknown HRESULT error is converted to a System.Runtime.InteropServices.COMException.




Type Library Registration

HKCR/TypeLib/{---}
HKLM/Software/Classes/Interfaces/{---}/TypeLib/Default - {--}
HKLM/Software/Classes/TypeLib/{---}



Application.RegisteredFunctions

Returns an array of all DLL functions that are registered within Excel.



Application.RegisterXLL(filename)

Loads an Excel DLL (xll) and registers it



VSTO add-ins load after all the other types of add-ins have loaded first.


You can easily distinguish between COM add-ins and VSTO add-ins in the registry because of the location information.
COM add-ins point to the DLL file
VSTO add-ins point to the .vsto file


Important

There is no deployment option that enables end users to use the COM Add-ins dialog box to install an add-in that has been created using VSTO.


© 2018 Better Solutions Limited. All Rights Reserved. © 2018 Better Solutions Limited

Top

Next