25. Automatisierung mit Excel-Daten
Wenn Daten in Excel wie in Excel-Datenverknüpfungen beschrieben bereitgestellt werden, können Sie die Funktionen UpdateChart
und PresentationFromTemplate
verwenden, um die Verwendung dieser Daten programmatisch zu kontrollieren.
Mit UpdateChart
ersetzen Sie das Datenblatt eines bestimmten Diagramms mit Ihren Daten. Mit PresentationFromTemplate
nutzen Sie Ihre Daten für die Erstellung einer neuen Präsentation, basierend auf einer PowerPoint-Vorlage, mit think-cell Diagrammen, die mit Datenbereichen in Excel verknüpft sind.
Die Schnittstelle zu beiden Funktionen ist in das Office Automation Model integriert, sodass sie mit jeder Programmiersprache für Office (z. B. Visual Basic for Applications oder C#) aufgerufen werden kann.
Der Einstiegspunkt in think-cell ist das think-cell Add-In-Objekt. Dieses kann über die Application.COMAddIns
-Sammlung aufgerufen werden. Aufrufe in think-cell sind immer spät gebunden. Eine Erklärung hierzu finden Sie in der Microsoft Knowledge Base unter:
http://support.microsoft.com/kb/245115
Der Typ des think-cell Add-In-Objekts ist somit einfach Object
, und es muss keine Typenbibliothek oder Referenz hinzugefügt werden. Sobald Sie sich das Objekt abgerufen haben, können Sie es aufrufen:
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
- 25.1
- DiagrammAktualisieren
- 25.2
- PräsentationAusVorlage
25.1 DiagrammAktualisieren
25.1.1 Unterschrift
tcaddin.UpdateChart( _
pres As PowerPoint.Presentation, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
25.1.2 Beschreibung
Diese Funktion, die aus Excel heraus aufgerufen wird, aktualisiert das Diagramm strName
in pres
mit den Zahlen aus rgData
. Der Bereich rgData
muss dem in Erstellen eines Diagramms aus Excel beschriebenen Layout entsprechen.
Beachten Sie, dass das Präsentationsobjekt pres
sich auch auf einen Folienbereich in der Präsentation beziehen kann. Weitere Informationen siehe:
http://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/slide-object-powerpoint
Beim Diagrammnamen strName
wird nicht zwischen Groß- und Kleinschreibung unterschieden. Er muss zuvor mithilfe der Eigenschaft UpdateChart-Name in PowerPoint zugewiesen worden sein, wie beschrieben in Einführung in die Automatisierung. Der Diagrammname muss innerhalb der Präsentation oder des Folienbereichs, der von pres
festgelegt wird, eindeutig sein.
Ist das Diagramm mit einem Excel-Datenbereich verknüpft, wenn die Funktion aufgerufen wird, wird die Verknüpfung getrennt. Anschließend besteht keine Verknüpfung zwischen dem Diagramm und dem Excel-Bereich mehr.
25.1.3 Beispiel
Um das Beispiel zu verwenden, klicken Sie in Excel im Fenster Visual Basic for Applications auf Extras, dann auf Verweise, und fügen Sie die „Microsoft PowerPoint Object Library“ hinzu.
' When Option Explicit appears in a file, you must
' explicitly declare all variables using the Dim
' or ReDim statements. If you attempt to use an
' undeclared variable name, an error occurs at
' compile time.
' Use Option Explicit to avoid incorrectly typing
' the name of an existing variable or to avoid
' confusion in code where the scope of the
' variable is not clear. If you do not use the
' Option Explicit statement, all undeclared
' variables are of Object type.
' http://msdn.microsoft.com/en-us/
' library/y9341s4f%28v=vs.80%29.aspx
Option Explicit
Sub UpdateChart_Sample()
' Get the range containing the new data
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets("Sheet1").Range("A1:D5")
' Get the think-cell add-in object
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim pres As PowerPoint.Presentation
' PowerPoint window visible
' Set pres = ppapp.Presentations.Open( _
' Filename:="c:\\template.pptx", _
' Untitled:=msoTrue)
' PowerPoint window invisible
Set pres = ppapp.Presentations.Open( _
Filename:="c:\\template.pptx", _
Untitled:=msoTrue, _
WithWindow:=msoFalse)
' The name "Chart1" must have been
' previously assigned to the chart using
' the control in the floating toolbar.
' The final argument indicates whether
' the data range is transposed or not.
Call tcaddin.UpdateChart(pres, "Chart1", rng, False)
' Save the updated presentation
pres.SaveAs ("c:\\template_updated.pptx")
pres.Close
ppapp.Quit
End Sub
Das nächste Beispiel zeigt, wie Sie UpdateChart
aus C# verwenden.
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
// Open the Solution Explorer > right-click the project file > "Add Reference..." button and add he following references:
// .NET tab > Microsoft.Office.Interop.Excel 12.0.0.0
// .NET tab > Microsoft.Office.Interop.PowerPoint 12.0.0.0
// COM tab > Microsoft Office 14.0 Object Library
namespace ConsoleApplication_UpdateChart
{
class Program
{
static void Main()
{
Excel.Application xlapp = new Excel.Application();
xlapp.Visible = true;
Excel.Workbook workbook = xlapp.Workbooks.Add(1);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
worksheet.Cells[3, 1] = "Series 1";
worksheet.Cells[3, 2] = 1;
worksheet.Cells[3, 3] = 2;
worksheet.Cells[3, 4] = 3;
PowerPoint.Application ppapp = new PowerPoint.Application();
PowerPoint.Presentation presentation =
ppapp.Presentations.Open("C://template.pptx", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue);
object[] aobjArg = new object[] { (object)presentation, "Chart1", worksheet.get_Range("A1", "D3"), false };
Office.COMAddIn comaddin = xlapp.COMAddIns.Item("thinkcell.addin");
object objAddIn = comaddin.Object;
objAddIn.GetType().InvokeMember("UpdateChart", System.Reflection.BindingFlags.InvokeMethod, null, objAddIn, aobjArg);
presentation.SaveAs("C://template_updated.pptx");
presentation.Close();
ppapp.Quit();
workbook.Close(false);
xlapp.Quit();
}
}
}
25.2 PräsentationAusVorlage
25.2.1 Unterschrift
tcaddin.PresentationFromTemplate( _
wb As Excel.Workbook, _
strTemplate As String, _
ppapp As PowerPoint.Application _
) As PowerPoint.Presentation
25.2.2 Beschreibung
Diese Funktion, die aus Excel heraus aufgerufen wird, fügt Datenverknüpfungen in wb
zur Vorlage mit dem Dateinamen strTemplate
hinzu. Das Ergebnis ist eine neue Präsentation innerhalb der PowerPoint-Instanz ppapp
.
strTemplate
kann entweder ein vollständiger oder relativer Pfadname sein. Der relative Pfadname ist relativ zum Ort der Excel-Arbeitsmappendatei wb
.
Alle Diagramme in strTemplate
, die mit der Excel-Arbeitsmappe wb
verknüpft sind, werden aktualisiert (egal ob sie auf automatische Aktualisierung eingestellt sind oder nicht). Anschließend werden ihre Datenverknüpfungen getrennt, um weitere Veränderungen dieser Diagramme zu verhindern.
Diagramme in strTemplate
, die mit anderen Excel-Arbeitsmappen als wb
verknüpft sind, bleiben unverändert und verknüpft. Es ist also möglich, Verknüpfungen mehrerer Excel-Arbeitsmappen zu aktualisieren, indem man das Ergebnis dieser Funktion als neue Vorlage speichert und die Funktion dann mit der nächsten Arbeitsmappe aufruft.
Falls Sie die Farben von Diagrammsegmenten mit der Excel-Verknüpfung steuern möchten, können Sie für das Farbschema die Option Excel-Füllung überlagern einstellen (siehe Farbschema). Um ebenso das Zahlenformat bei der Excel-Verknüpfung zu steuern, setzen Sie es auf Excel-Format verwenden (siehe Zahlenformat). Stellen Sie sicher, dass Sie die Hintergrundfarbe und das Zahlenformat der entsprechenden Zellen in Excel eingestellt haben, bevor Sie PresentationFromTemplate
aufrufen.
25.2.3 Beispiel
Um das Beispiel zu verwenden, klicken Sie in Excel im Fenster Visual Basic for Applications auf Extras, dann auf Verweise, und fügen Sie die „Microsoft PowerPoint Object Library“ hinzu.
' When Option Explicit appears in a file, you must
' explicitly declare all variables using the Dim
' or ReDim statements. If you attempt to use an
' undeclared variable name, an error occurs at
' compile time.
' Use Option Explicit to avoid incorrectly typing
' the name of an existing variable or to avoid
' confusion in code where the scope of the
' variable is not clear. If you do not use the
' Option Explicit statement, all undeclared
' variables are of Object type.
' http://msdn.microsoft.com/en-us/
' library/y9341s4f%28v=vs.80%29.aspx
Option Explicit
Sub PresentationFromTemplate_Sample()
' Get the range to modify. It is more efficient
' to do this once rather than within the loop.
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets("Sheet1").Cells(3, 2)
' Get the think-cell add-in object
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim i As Integer
For i = 1 To 10
' Modify the range value.
' Note: Avoid selecting the cell prior to
' changing it. It is very slow and has
' undesirable side-effects.
' BAD:
' rng.Select
' ActiveWindow.Selection.Value = 0
rng.Value = i
' Generate a new presentation based on the
' linked template.
Dim pres As PowerPoint.Presentation
Set pres = tcaddin.PresentationFromTemplate( _
Excel.ActiveWorkbook, "template.pptx", ppapp)
' If you want to modify the new presentation
' before saving it this is the place to do it.
' Save the new presentation
pres.SaveAs "c:\\output" & i & ".pptx"
' Explicitly close the presentation when we
' are done with it to free its memory.
' Letting the object go out of scope is not
' sufficient.
pres.Close
Next
End Sub