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

[ServiceContract(Namespace=”http://WCFMSOfficeTest.com”)]
public interface IMSOfficeTestService
{
[OperationContract]
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=””http://WCFMSOfficeTest.com””,”
address= address+ “binding=””BasicHttpBinding_IMSOfficeTestService””, bindingNamespace=””http://WCFMSOfficeTest.com”””

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s