0

How can I use my C# project within Excel (VBA) when it is in executable form?

In Visual Studio, if I go to my project settings and change the "Output Type" to "Class Library", then I can successfully add a reference to my project in Excel VBA (by browsing to the DLL in the references section).

However, when my "Output Type" is set to "Windows Application", if I attempt to add a reference (within Excel VBA) to the newly generated EXE file, I receive the following error:

Can't add a reference to the specified file

How can I reference my .NET EXE within Excel VBA?

(My purpose for using an exe rather than a DLL is so that my application can be used both as a Windows Application and as a COM object)

1 Answer 1

2

Without knowing the exact details I simply guess that a DLL exports functions that are required by Excel. An EXE usually does not export any functions.

The good thing about .NET is that you can easily create modular application. So why not put the core functionality into a DLL and create an application that can use the functionality?

That way you could use the DLL from VBA and still have an application you can use standalone.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. I'm not sure if this guy is correct but he claims that you can reference a .Net executable as if it were a dll anyway, I would prefer not to deploy 2 separate items if possible. It would be much cleaner if I can just send out one single exe.
You can add any .NET assembly as a reference in any other .NET project, that's true. .NET doesn't care whether it is an assembly with .exe or .dll or .blurb extension, as long as it is a .NET assembly. But that is something significantly different than using a .NET DLL in Excel VBA!
Got it. I seem to remember that VBA could reference a VB6 EXE but perhaps something similar is not possible with .NET <=> VBA communication. There is actually an option in Assembly Information of the .NET project properties which allows you to set "Make assembly COM-Visible". This option is also enabled for Windows Applications but perhaps it only actually applies to DLLs.
To reference a VB6 EXE it had to be compiled as an "ActiveX Executable" which was in reality an out-of-process COM server, you could do this in .Net but its clunky and no where near as nice as Thorstens suggestion of a common assembly and satellite .Net EXE.
OK thanks for the suggestions - for now I will deploy a separate DLL but in future will look at sharing this into one code base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.