Chart Title in Excel Add-in code example

Chart Title in Excel Add-in code example

ChartTitle

An Object which exposes various underlying properties and methods, helps manipulate chart title in Excel Sheet. In this example we will grab a chart and change the title and apply formatting over Chart Title using C#, VB.Net VSTO (Visual Studio Tools for Office) and VBA (Visual Basic for Applications):

C# code example

private void btnChartTitle_Click(object sender, RibbonControlEventArgs e)
{
	//bind worksheet reference
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;

	//bind chart collection
	excel.ChartObjects oChartObjects = oWorksheet.ChartObjects();

	//get chart from collection
	excel.ChartObject oChartObject = oChartObjects.Item(1);

	//Get chart
	excel.Chart oChart = oChartObject.Chart;

	//activate the chart
	oChartObject.Activate();

	//enable title
	oChart.HasTitle = true;

	//bind chart title object
	excel.ChartTitle oChartTitle = oChart.ChartTitle;

	//change text
	oChartTitle.Caption = "VBAOVERALL";
	oChartTitle.Font.Name = "Arial";
	oChartTitle.Font.Size = 10;
	oChartTitle.Font.Bold = true;
	oChartTitle.Font.Color = excel.XlRgbColor.rgbBlue;
}

VB.Net code example

Private Sub btnChartTitleExample_Click(sender As Object, e As RibbonControlEventArgs) Handles btnChartTitleExample.Click
	'bind worksheet reference
	Dim oWorksheet as excel.Worksheet
	oWorksheet = Globals.ThisAddIn.Application.ActiveSheet

	'bind chart collection
	Dim oChartObjects As excel.ChartObjects
	oChartObjects = oWorksheet.ChartObjects()

	'get chart from collection
	Dim oChartObject as excel.ChartObject
	oChartObject = oChartObjects.Item(1)

	'Get chart
	Dim oChart As excel.Chart
	oChart = oChartObject.Chart

	'activate the chart
	oChartObject.Activate()

	'enable title
	oChart.HasTitle = True

	'bind chart title object
	Dim oChartTitle As excel.ChartTitle
	oChartTitle = oChart.ChartTitle

	'change text
	oChartTitle.Caption = "VBAOVERALL"
	oChartTitle.Font.Name = "Arial"
	oChartTitle.Font.Size = 10
	oChartTitle.Font.Bold = True
	oChartTitle.Font.Color = excel.XlRgbColor.rgbBlue
End Sub

VBA code example

Public Sub ChartTitleObjectExample()
    'bind worksheet reference
    Dim oWorksheet As Worksheet
    Set oWorksheet = ActiveSheet

    'bind chart collection
    Dim oChartObjects As ChartObjects
    Set oChartObjects = oWorksheet.ChartObjects

    'get chart from collection
    Dim oChartObject As ChartObject
    Set oChartObject = oChartObjects.Item(1)

    'Get chart
    Dim oChart As Chart
    Set oChart = oChartObject.Chart

    'activate the chart
    oChartObject.Activate

    'enable title
    oChart.HasTitle = True

    'bind chart title object
    Dim oChartTitle As ChartTitle
    Set oChartTitle = oChart.ChartTitle

    'change text
    oChartTitle.Caption = "VBAOVERALL"
    oChartTitle.Font.Name = "Arial"
    oChartTitle.Font.Size = 10
    oChartTitle.Font.Bold = True
    oChartTitle.Font.Color = Excel.XlRgbColor.rgbBlue
End Sub

Output before code execution

figure 1.0

Output post code execution

figure 1.1

Next >> How to Create Custom Axis Chart in Excel? Example

Leave a Reply

Your email address will not be published.