The PODA Blog

News, views and articles from our membership

Archive for September 6th, 2007

Creating and Deploying Managed COM Add-ins with VSTO 2005 SE - Part IV

Posted by Dennis Wallentin on 6th September 2007

To read previous blog posts on the subject please see:

Here in the fourth blog post on the subject focus will be on the procedures that make up the utility. In order to take part of the code please sees the following article at my English site: Creating and Deploying Managed COM Add-ins with VSTO SE – Part III

The most notable aspect of the code is that in order to access the Excel Object model we need to go via the ThisAddin class:

  • If inside the ThisAddin class we can access it with the statement:

Visual Basic:
Me.Application

  • Code outside the class but inside the project we can access it with the statement:

Visual Basic:
Globals.ThisAddin.Application

This is the major difference between VSTO solutions and VB.NET (Shared Add-in) solutions (except for the differences in the connection class). As You will see in the code article at my site (see above) the chart object is created and manipulated via Excel’s unmanaged Object model (see the Create_Chart_Report procedure). However, in addition to the host controls NamedRange and ListObject the chart host control is also available via Microsoft.Excel.Office.Tools.Excel. In other words, we can manage the chart objects through managed code.

The following example shows how we can create and manipulate a chart object in VSTO. Here I use a customized Workbook solution where the chart is created on one worksheet:

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
Const p_Title As String = "Volume / Product"<br />
Dim p_LastCell As Long = _<br />
Me.Cells(Me.Rows.Count, 2).End(Excel.XlDirection.xlUp).Row<br />
Dim p_Data As Excel.Range = Me.Range("A1:B" & p_LastCell)<br />
Dim p_Chart As Microsoft.Office.Tools.Excel.Chart = _<br />
Me.Controls.AddChart(left:=200, top:=25, width:=500, height:=225, name:=p_Title)</p>
<p>With p_Chart<br />
.SetSourceData(Source:=p_Data, PlotBy:=Excel.XlRowCol.xlColumns)<br />
.ChartType = Excel.XlChartType.xl3DBarClustered<br />
.ChartTitle.Caption = p_Title<br />
With .ChartArea<br />
.Border.LineStyle = 0<br />
With .Font<br />
.Name = "Tahoma"<br />
.Size = 8<br />
End With<br />
End With<br />
.Legend.Delete()<br />
.PlotArea.Interior.ColorIndex = 20<br />
.SeriesCollection(1).Interior.ColorIndex = 40<br />
.Axes(Excel.XlAxisType.xlCategory).TickLabelSpacing = 1<br />
End With<br />
End Sub

In the next post I will discuss the deployment process which has considerable been updated in version 3.0 of VSTO.

Kind regards,

Dennis

Posted in Office (All), VSTO | No Comments »