Adding the Action Pane to the Spreadsheet
Now that you have a working action pane, you need to add it to your spreadsheet, and trigger it to activate when you use your spreadsheet.
Within
Sheet1.cs, you need to declare the action pane by making it a member variable of the class and activating it on startup as shown below:
public partial class Sheet1
{
StockResearch sr = new StockResearch();
private void Sheet1_Startup(object sender, System.EventArgs e)
{
Globals.ThisWorkbook.ActionsPane.Controls.Add(sr);
}
Now, you can trigger an update to the action pane whenever the user changes an item on the sheet by calling the update method from the
Sheet1_Change event. It's very simple, you just pull the value of the
Target argument passed to that event-handler, which is the selected range in Excel. You then pass this Range to the StockResearch research pane's
GetUpdatedData function which gets the data and updates the Excel pane. You will then see the results from
Figure 2.
private void Sheet1_Change(
Microsoft.Office.Interop.Excel.Range Target)
{
string strTicker = Target.Text.ToString();
sr.GetUpdatedData(strTicker);
}
}
As with Outlook, you can use Excel as a terrific vehicle for delivering your applications. In a situation where you have to deliver research data to your users, they may already be using Excel, and as such by using it, not only will you gain the benefits of a desktop runtime environment that you can deliver your app to, but you'll also be able to enhance their user experience by keeping your application and data inline with theirs. This article presented an example scenario where financial users might be using spreadsheets to manage their portfolios. Using VSTO, you can deliver your data enhancements directly into their spreadsheets by capitalizing on the Action Pane concept. And the best part is that doing so is both straightforward and uses your existing .NET coding skillsdeveloping user controls. In other words, you don't even need to learn a new API to build Action Panes within Office apps.