Saturday, May 16, 2009

Excel Addin : calling service

Given a simple asmx service, here is how to call it in an Excel AddIn


(...)
using ExcelAddIn.ServiceReference1;

namespace ExcelAddIn
{
public partial class ThisAddIn
{

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

public void Hello()
{
using (Service1SoapClient svc = new Service1SoapClient())
{
Excel.Range rgStart;
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
rgStart = ws.Application.ActiveCell;
rgStart.Value2 = svc.HelloWorld();
}
}


#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}



In RibbonBar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;

namespace ExcelAddIn
{
public partial class Ribbon1 : OfficeRibbon
{
public Ribbon1()
{
InitializeComponent();
}

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{

}

private void button1_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.Hello();
}
}
}

No comments:

Post a Comment