25. Excelデータを使用しての自動化
Excel データ リンクで示すようにデータがExcel に入力されているとき、UpdateChart
とPresentationFromTemplate
の機能を使って、プログラムに従いそのデータ使用を制御することができます。
UpdateChart
の機能により、グラフのデータシートをお使いのデータと交換することができます。PresentationFromTemplate
の機能により、 Excel のデータ範囲にリンクされたthink-cell のグラフで、PowerPoint テンプレートに基づくプレゼンテーションを新たに作成するに当たりお使いのデータを使用することができます。
これらの2つの機能のインターフェースは Office Automation モデルに統合されているため、Visual Basic for Applications や C# など Office のプログラミングを可能にする言語であれば、どんな言語からもアクセスできます。
think-cell へのエントリ ポイントは、think-cell アドイン オブジェクトです。これには Application.COMAddIns
コレクションからアクセスできます。think-cell への呼び出しは、常に遅延結合です。詳細は Microsoft のサポート技術情報を参照してください。
http://support.microsoft.com/kb/245115
このように、think-cell アドイン オブジェクトのタイプは単に Object
であり、追加するタイプ ライブラリまたは参照はありません。オブジェクトを取得さえすれば、すぐに呼び出しができます:
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
- 25.1
- グラフの更新
- 25.2
- テンプレートからのプレゼンテーション
25.1 グラフの更新
25.1.1 署名
tcaddin.UpdateChart( _
pres As PowerPoint.Presentation, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
25.1.2 説明
Excel から呼び出されるこの関数は、pres
のグラフ strName
を rgData
に含まれる数字で更新します。範囲 rgData
は、Excel からのグラフ作成に記載されているレイアウトに準拠している必要があります。
プレゼンテーションのオブジェクトpres
を使用して、プレゼンテーションのスライドの範囲を参照することもできる点に留意してください。詳細については以下をご覧ください:
http://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/slide-object-powerpoint
グラフ名 strName
は大文字と小文字を区別して整合されます。この機能は以前、 オートメーション機能のご紹介にあるUpdateChart Name プロパティ コントロールを使用してPowerPoint に割り当てられていました。グラフ名は、プレゼンテーション内、またはpres
で定義されたスライド範囲内で一意のものでなくてはなりません。
この関数を呼び出すときにグラフが何らかの Excel データの範囲にリンクされていると、リンクが切断されます。その後、グラフはいかなる Excel の範囲にもリンクされません。
25.1.3 例
このサンプルを使用するには、Excel の Visual Basic for Applications のウィンドウで、[ツール]、[参照] にアクセスし、Microsoft PowerPoint Object Library を追加します。
' 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
次のサンプルは、C# の UpdateChart
を使用する方法を示しています。
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 テンプレートからのプレゼンテーション
25.2.1 署名
tcaddin.PresentationFromTemplate( _
wb As Excel.Workbook, _
strTemplate As String, _
ppapp As PowerPoint.Application _
) As PowerPoint.Presentation
25.2.2 説明
Excel から呼び出されるこの関数は、wb
のデータ リンクをファイル名 strTemplate
でテンプレートに適用します。その結果、PowerPoint インスタンス ppapp
内に新しいプレゼンテーションが作成されます。
strTemplate
は完全なパスでも相対パスでもかまいませんが、その後、Excel のブックファイル wb
の場所に関連付けられます。
Excel ブック wb
にリンクされている strTemplate
のすべてのグラフは、(自動更新に設定されているかどうかに関係なく) 更新されます。その後、こうしたグラフへのさらなる変更を予防するため、データ リンクが切断されます。
wb
以外の Excel ブックにリンクされている strTemplate
のグラフは変更されずにリンクされているため、この関数の結果を新しいテンプレートとして保存して次のブックでこの関数を再度呼び出すことで、複数の Excel ブックからリンクを更新できます。
Excel リンクを使用してグラフ セグメントの色をコントロールする場合は、Excel の塗りつぶしを使用するに配色を設定できます (配色参照)。同様に、Excel リンクで表示形式をコントロールするには、[Excel の書式設定を使用する] に設定します (数値形式参照)。PresentationFromTemplate
を呼び出す前に、Excel の各セルの背景色と数値形式を設定してください。
25.2.3 例
このサンプルを使用するには、Excel の Visual Basic for Applications のウィンドウで、[ツール]、[参照] にアクセスし、Microsoft PowerPoint Object Library を追加します。
' 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