How to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA: Guided Minimum Framework Example

Dev

Summary:

A minimum working example of how to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA or another C# application.

 

This is a follow up to the non GUI example posted earlier

 

Project: AutomationComExample.zip

 

  • 1 Summary:
  • 2 Framework Structure
    • 2.1 ExampleTargetApp (GUI Application)
      • 2.1.1 MainWindow.xaml.cs
      • 2.1.2 HelloWorldService.cs
    • 2.2 ServiceDefinition
      • 2.2.1 IHelloWorldService.cs
    • 2.3 CallingDll
      • 2.3.1 ServiceProxy.cs
      • 2.3.2 ICallTargetApp.cs
      • 2.3.3 CallTargetApp.cs
  • 3 Example access from VBA

Framework Structure

The goal is to connect the piping features of WCF named piping class with the GUI features of the MainWindow class. C# does not support multiple inheritance, but that is fine. We only need an interface and C# supports that of course. We can create an interface from the GUI that the DLL will utilize to allow changes to the GUI from another application.

The framework consists of three projects within visual studio: ExampleTargetApp, ServiceDefinition, and CallingDll.

  • ExampleTargetApp contains the information for the GUI and handles what happens when elements of the GUI interact with the user, presumably via mouse and keyboard but could also be via function piping. This project also contains a service to make use of the pipes interface.
  • ServiceDefinition contains an interface for the functions to go through WCF pipes.
  • CallingDll uses the service interface to access functionality contained and defined in the ExampleTargetApp project

ExampleTargetApp (GUI Application)

MainWindow.xaml.cs

Contains the elements of the GUI such as Button1_Click() and SelectRadio1()

HelloWorldService.cs

Contains the service class using the service interface

ServiceDefinition

IHelloWorldService.cs

Contains the service interface definition

CallingDll

ServiceProxy.cs

Set up the service to work using the service contract defined in the ServiceDefinition project

ICallTargetApp.cs

Interface for calling DLL

CallTargetApp.cs

Functions for calling DLL through the proxy

 

Example access from VBA

vbacallingdll

Load the dll “CallingDll” by selecting browse then going to the project build directory: C:\dev\AutomationComExample\CallingDll\bin\x86\Debug\CallingDll.tlb

Excel VBA Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sub SelectHello()
    Dim targetApp As Object
    Set targetApp = CreateObject("CallingDll.CallTargetApp")
    targetApp.SelectHello
End Sub
Sub SelectGoodbye()
    Dim targetApp As Object
    Set targetApp = CreateObject("CallingDll.CallTargetApp")
    targetApp.SelectGoodbye
End Sub
Sub SelectButton()
    Dim targetApp As Object
    Set targetApp = CreateObject("CallingDll.CallTargetApp")
    targetApp.SelectButton
End Sub

One thought on “How to tie a C# .NET GUI to a callable C# .NET class library dll from Excel VBA: Guided Minimum Framework Example

Leave a Reply

Your email address will not be published. Required fields are marked *