25. Automation with Excel data
When data is provided in Excel, you can use the functions UpdateChart
and PresentationFromTemplate
to programmatically control using that data.
With UpdateChart
you exchange the datasheet of a specific element with your Excel data. With PresentationFromTemplate
you use your data to create a new presentation based on a PowerPoint template with think-cell elements linked to data ranges in Excel as described in 21. Excel data links.
The interface to both functions is integrated into the Office Automation model, so it can be accessed from any language with which you can program Office, such as Visual Basic for Applications (VBA) or C#. See F.1 Getting started for detailed instructions.
The entry point into think-cell is the think-cell add-in object. It can be accessed via the Application.COMAddIns
collection. Calls into think-cell are always late-bound. See Microsoft’s knowledge base for an explanation:
Using early binding and late binding in Automation
Thus, the type of the think-cell add-in object is simply Object
, and there is no type library or reference to add. Just acquire the object, and you are ready to make calls. For example, in VBA in Excel:
In C#, you can achieve late binding by declaring the reference to the think-cell add-in object as dynamic
; this is also the type inferred by the compiler when declaring the reference as var
, so that you can simply write:
Here xlapp
is a reference to an Excel.Application
object in which think-cell is loaded.
Contents
- 25.1
- UpdateChart
- 25.2
- PresentationFromTemplate
25.1 UpdateChart
25.1.1 Signature
VBA
C#
25.1.2 Description
This function updates all elements in target
with name strName
with the data contained in rgData
. For the data to be interpreted correctly for charts, the range rgData
must conform to their configured datasheet layout, or its transposed version, see also 21.1 Creating a chart from Excel and 21.2 Fitting the data layout. Whether the default or the transposed version is to be used is indicated by setting bTransposed
to false
or true
, respectively. For elements other than charts, tables for example, the value of bTransposed
is ignored.
target
must be a Presentation
or SlideRange
, or a single Slide
, Master
or CustomLayout
.
The name strName
is matched case-insensitively. It must have been previously assigned in PowerPoint using the UpdateChart Name property control as described in 24. Introduction to automation.
To ensure that the correct elements are targeted, make sure that they are the only ones with their UpdateChart Name set to strName
in the object passed as pres
.
If a targeted element is linked to an Excel data range when invoking this function, the link is broken. Afterwards, the element will not be linked to any Excel range.
25.1.3 Examples
To use these samples, prepare a presentation as described in 24. Introduction to automation, and save it as C:\Samples\UpdateChart\template.pptx
.
VBA
To use this sample add it to a module in an Excel workbook.
It requires a reference to the Microsoft PowerPoint 16.0 Object Library (see F.1.1 Visual Basic for Applications for details).
Running UpdateChart_Sample
in a workbook will update the chart in the presentation template with the data contained in range A1:D5
of its first sheet.
C#
To use this sample, replace the code in Program.cs
of the C# Console App project template with it.
It requires references to the Microsoft PowerPoint 16.0 Object Library, the Microsoft Excel 16.0 Object Library and the Microsoft Office 16.0 Object Library (see F.1.2 C# for details).
Running the resulting application will update the chart in the presentation template to contain a single series named "Series 1" with values 1, 2, 3, and save the result as template_updated.pptx
.
25.2 PresentationFromTemplate
25.2.1 Signature
VBA
C#
25.2.2 Description
This function uses the data links between the Excel workbook wb
and the template with filename strTemplate
to instantiate that template by updating the linked elements with the data from the ranges they are linked to. The result is a new presentation within the PowerPoint instance ppapp
.
strTemplate
can either be a full path or a relative path, which is then taken to be relative to the location of the Excel workbook file wb
.
All elements in strTemplate
that are linked to the Excel workbook wb
are updated (regardless whether they are set to auto-update or not). In the resulting presentation, their data links are broken to prevent further changes to these elements.
Elements in strTemplate
which are linked to Excel workbooks other than wb
are left unchanged and still linked, so it is possible to update links from multiple Excel workbooks by saving the result of this function as a new template and then calling this function again with the next workbook.
If you want to control the colors of chart segments or the formatting of table cells with the Excel link, you can set the color scheme to Use Datasheet Fill on Top (see 3.8.2 Color scheme) or the Use Datasheet... options (see 17.3 Formatting a table), respectively. Likewise, to control the number format with the Excel link, set it to Use Excel Format (see 6.5.3 Number format).
Make sure to set the relevant formatting options and the number format of the respective cells in Excel before calling PresentationFromTemplate
.
25.2.3 Examples
To use these samples, first create a presentation containing a stacked chart linked to the range G1:K4
of the first sheet in an Excel workbook as explained in 21.1 Creating a chart from Excel. Save the resulting presentation as C:\Samples\PresentationFromTemplate\template.pptx
and the workbook as data.xlsx
in the same directory.
VBA
To use this sample, add it to a module in the Excel workbook data.xlsx
prepared as explained above.
It requires a reference to the Microsoft PowerPoint 16.0 Object Library (see F.1.1 Visual Basic for Applications for details).
Running PresentationFromTemplate_Sample
will change the value in cell Sheet1!H3
, which is linked to the first value of the first series of the chart contained in template.pptx
, from i=1
to 10
, create a new presentation with the chart in the template updated to contain that value, and which is not linked to the workbook anymore, and save it as output_i.pptx
in the same directory as the template.
C#
To use this sample replace the code in Program.cs
of the C# Console App project template with it.
It requires references to the Microsoft PowerPoint 16.0 Object Library, the Microsoft Excel 16.0 Object Library and the Microsoft Office 16.0 Object Library (see F.1.2 C# for details).
Running the resulting application will visibly open Excel, load the workbook data.xlsx
, change the value in cell H3
, which is linked to the first value of the first series of the chart contained in template.pptx
, from i=1
to 10
, create a new presentation with the chart in the template updated to contain that value, and which is not linked to the workbook anymore, and save it as output_i.pptx
in the same directory as the template.