Visual C++ 5.0 および Visual C++ 6.0 で Microsoft Foundation Classes を使用して PowerPoint を自動化する方法
| 文書番号 | : | 237554 | | 最終更新日 | : | 2008年3月18日 | | リビジョン | : | 5.0 |
概要
この資料では、Microsoft Visual C++ 5.0 および Visual C++ 6.0 で MFC (Microsoft Foundation Classes) を使用して Microsoft PowerPoint を自動化する方法について説明します。
Microsoft Foundation Classes を使用して、PowerPoint を自動化することや、プレゼンテーションに含まれているマクロを実行することができます。
先頭へ戻る
詳細
通常は、クラス ウィザードを使用してタイプ ライブラリ内のすべての関数をラップできます。ただし、クラス ウィザードでは SAFEARRAY 型の引数を必要とする関数はラップできず、ヘッダー ファイル内に次のようなメッセージが生成されます。
// method 'MyMethod' not emitted because of invalid return type or parameter type
クラス ウィザードを使用して Microsoft PowerPoint 用のラッパー クラスを生成する場合、Application::Run 関数は SAFEARRAY 型の引数を取るためラップされません。DISPID に 0x7e6 を指定して InvokeHelper 関数を使用して Application::Run を呼び出すことができます。Run 関数の詳細については、OLE/COM オブジェクト ビューアで PowerPoint タイプ ライブラリを参照してください。
次の手順では、Application::Run を呼び出して PowerPoint マクロを実行するように PowerPoint を自動化する方法を説明しています。
マクロを含む PowerPoint プレゼンテーションを作成する手順
| 1. |
Microsoft PowerPoint を起動し、新しいプレゼンテーションに 3 つの空白のスライドを作成します。
| | 2. |
Alt + F11 キーを押して、Visual Basic Editor を起動します。
| | 3. |
[挿入] メニューの [標準モジュール] をクリックして、新しいプレゼンテーションに標準モジュールを挿入します。
| | 4. |
モジュールに次のコードを追加します。
Sub ChangeBackColor() 'Change backcolor of slides 1, 2 and 3
With ActivePresentation.Slides.Range(Array(1, 2, 3))
.FollowMasterBackground = msoFalse
.Background.Fill.ForeColor.SchemeColor = ppAccent2
End With
End Sub
Sub ChangeText(vArray As Variant)
'Add a text box to each slide and use the text from the
'array passed into this procedure
Dim s As Slide
For i = 1 To ActivePresentation.Slides.Count
With ActivePresentation.Slides(i).Shapes.AddTextbox( _
msoTextOrientationHorizontal, 186#, 54#, 336#, 36#)
.TextFrame.TextRange.Text = vArray(i - 1)
.TextFrame.TextRange.Font.Size = 60
End With
Next
End Sub
| | 5. |
プレゼンテーションを次のいずれかの方法で保存し、PowerPoint を閉じます。
| ? |
PowerPoint 2007 では、C:\Pres.pptm などのファイル名を使用して PowerPoint マクロ有効プレゼンテーション (*.pptm) としてプレゼンテーションを保存します。
| | ? |
PowerPoint 2003 またはそれ以前のバージョンの PowerPoint では、C:\Pres.ppt などのファイル名を使用してプレゼンテーション (*.ppt) として保存します。
|
|
MFC アプリケーションの作成手順
| 1. | AutoPPT という名前の MFC ダイアログ ボックスベースの新しいアプリケーションを作成します。
| | 2. |
[表示] メニューの [ClassWizard] をクリックし (または Ctrl + W キーを押し)、次の手順を実行します。
| a. |
[オートメーション] タブをクリックします。
| | b. |
[クラスの追加] をクリックし、[タイプ ライブラリから] をクリックします。
| | c. |
PowerPoint ライブラリを検索し、PowerPoint タイプ ライブラリを追加します。
| ? |
PowerPoint 2007 では、[Msppt.olb] を追加します。
| | ? |
PowerPoint 2003 では、[Msppt.olb] を追加します。
| | ? |
PowerPoint 2002 では、[Msppt.olb] を追加します。
| | ? |
PowerPoint 2000 では、[Msppt9.olb] を追加します。
| | ? |
PowerPoint 97 では、[Msppt8.olb] を追加します。
|
|
| | 3. |
リソース ID IDD_AUTOPPT_DIALOG のダイアログ ボックスを選択します。ダイアログ ボックスにボタンを追加し、ボタンのハンドラに次のコードを追加します。
_Application oApp;
if(!oApp.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Could not get Powerpoint application.");
return;
}
oApp.SetVisible(TRUE);
//Get the Presentations collection and open a presentation
Presentations oPresSet(oApp.GetPresentations());
CString strFilename;
//For PowerPoint 2007, change the file name to "c:\\pres.pptm"
strFilename = "c:\\pres.ppt";
_Presentation oPres(oPresSet.Open(strFilename, // Filename
true, // Readonly
false, // Untitled
true // WithWindow
));
//*************** How to Run PowerPoint Macros *********************
// Run "ChangeBackColor" macro in the presentation -- note that the
// "ChangeBackColor" macro requires no arguments
{
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
static BYTE parms[] =
VTS_BSTR VTS_VARIANT;
LPCTSTR lpszMacroName = "Pres.ppt!ChangeBackColor";
oApp.InvokeHelper(0x7e6, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
lpszMacroName, //Macro Name
&vOpt //No arguments, for example, ignore
);
}
// Run "ChangeText" macro in the presentation;
// The macro requires three arguments -- the first two are strings
// and the last one is a long
{
COleSafeArray saArgs; //Create a 1-dimensional array with three
//elements
DWORD numElements[1];
numElements[0]= 3;
saArgs.Create(VT_VARIANT, 1, numElements);
long index[1];
VARIANT v;
index[0]=0; //Fill 1st element
CString sName("ABC");
VariantInit(&v);
v.vt= VT_BSTR;
v.bstrVal = sName.AllocSysString();
saArgs.PutElement(index, &v);
SysFreeString(v.bstrVal);
VariantClear(&v);
index[0]=1; //Fill 2nd element
CString sCompany("XYZ");
VariantInit(&v);
v.vt= VT_BSTR;
v.bstrVal = sCompany.AllocSysString();
saArgs.PutElement(index, &v);
SysFreeString(v.bstrVal);
VariantClear(&v);
index[0]=2; //Fill 3rd element
VariantInit(&v);
v.vt = VT_I4;
v.lVal=123;
saArgs.PutElement(index, &v);
VariantClear(&v);
static BYTE parms[] =
VTS_BSTR VTS_VARIANT;
LPCTSTR lpszMacroName = "Pres.ppt!ChangeText";
oApp.InvokeHelper(0x7e6, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
lpszMacroName, //Macro Name
(VARIANT*)saArgs //Array of macro parameters
);
saArgs.Detach();
}
注 : PowerPoint 2002 タイプ ライブラリのクラスをラップする場合は、Open メソッドを呼び出すために引数 (false) を追加します。
| | 4. |
Powerpoint ヘッダー ファイル用の適切な include ステートメントを追加します。
#include "msppt8.h" // use msppt9.h for 2000 or msppt.h for 2002, 2003, and 2007
| | 5. |
CAutoPPTApp::InitInstance() 関数の先頭に次のコード行を追加します。
AfxOleInit();
| | 6. |
コンパイルして実行します。ダイアログ ボックスに追加したボタンをクリックして自動化コードを実行します。コードが完了すると PowerPoint が表示されたままとなり、マクロによってプレゼンテーションに加えられた変更を確認できます。
|
先頭へ戻る
関連情報
Visual C++ を使用した PowerPoint の自動化の関連情報については、以下の「サポート技術情報」 (Microsoft Knowledge Base) を参照してください。
180616 (http://support.microsoft.com/kb/180616/) MFC を使用して、PowerPoint プレゼンテーションを作成し、表示する方法
先頭へ戻る
この資料は以下の製品について記述したものです。| ? | Microsoft Office PowerPoint 2007 | | ? | Microsoft Office PowerPoint 2003 | | ? | Microsoft PowerPoint 2002 Standard Edition | | ? | Microsoft PowerPoint 2000 Standard Edition | | ? | Microsoft PowerPoint 97 Standard Edition | | ? | Microsoft Visual C++ 6.0 Professional Edition | | ? | Microsoft Visual C++ 5.0 Professional Edition | | ? | Microsoft Foundation Class Library 4.2 |
先頭へ戻る
| kbexpertiseinter kbautomation kbhowto KB237554 |
先頭へ戻る
"Microsoft Knowledge Baseに含まれている情報は、いかなる保証もない現状ベースで提供されるものです。Microsoft Corporation及びその関連会社は、市場性および特定の目的への適合性を含めて、明示的にも黙示的にも、一切の保証をいたしません。さらに、Microsoft Corporation及びその関連会社は、本文書に含まれている情報の使用及び使用結果につき、正確性、真実性等、いかなる表明・保証も行ないません。Microsoft Corporation、その関連会社及びこれらの権限ある代理人による口頭または書面による一切の情報提供またはアドバイスは、保証を意味するものではなく、かつ上記免責条項の範囲を狭めるものではありません。Microsoft Corporation、その関連会社 及びこれらの者の供給者は、直接的、間接的、偶発的、結果的損害、逸失利益、懲罰的損害、または特別損害を含む全ての損害に対して、状況のいかんを問わず一切責任を負いません。(Microsoft Corporation、その関連会社 またはこれらの者の供給者がかかる損害の発生可能性を了知している場合を含みます。) 結果的損害または偶発的損害に対する責任の免除または制限を認めていない地域においては、上記制限が適用されない場合があります。なお、本文書においては、文書の体裁上の都合により製品名の表記において商標登録表示、その他の商標表示を省略している場合がありますので、予めご了解ください。" | Other Support Options - Contact Microsoft
Phone Numbers, Support Options and Pricing, Online Help, and more. - Customer Service
For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more. - Newsgroups
Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.
|
|