Calling WCF Service inside MS Office applications

One of the easier ways to call a WCF Service inside MS Office applications (Word/Excel) is using the WCF Service moniker component.

Let us see how we can do this.

1. Create a sample WCF Service

public interface IMSOfficeTestService
int AddData(int value1, int value2);

2. Implement the Service

public class MSOfficeTestService: IMSOfficeTestService
public int AddData(int value1, int value2)
return value1+value2;

3. Host the service with BasicHttpBinding and open the Mex Endpoint.

4. Create a Macro

4.1 Open Excel -> Developer -> Macros -> Create. Name it WCFCall
Sub WCFCall()

Dim address As String

address= “service:mexAddress=’http://localhost:8500/MSOfficeTestService/mex’,” ‘metadata location
address= address+ “address=””http://localhost:8500/MSOfficeTestService/””,” ‘service location
address= address+ “contract=””IMSOfficeTestService””, contractNamespace=””””,”
address= address+ “binding=””BasicHttpBinding_IMSOfficeTestService””, bindingNamespace=”””””

Dim wcfscv As Object
Set wcfscv As = GetObject(address)
Dim value1 As Integer

Dim value2 As Integer
value1 = Sheet1.Cells(1, 1)

value2 = Sheet1.Cells(1, 2)

Sheet1.Cells(1, 3) = wcfscv.GetData(val1, val2)
End Sub

5.  Enter values in A1 & B1

6. Run the Macro

7. Results will be displayed in C1.

It is an easy way and if you want to get the data from a WCF Service directly into excel for calculations this is one of the way to do it.