Microsoft KB Archive/237554

= How to automate PowerPoint by using Microsoft Foundation Classes in Visual C++ 5.0 and in Visual C++ 6.0 =

Article ID: 237554

Article Last Modified on 5/24/2007

-

APPLIES TO


 * 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

-



This article was previously published under Q237554



SUMMARY
This article describes how to automate Microsoft PowerPoint by using Microsoft Foundation Classes (MFC) in Microsoft Visual C++ 5.0 and in Visual C++ 6.0.

You can use Microsoft Foundation Classes to automate PowerPoint and to run a macro that is contained in a presentation.



MORE INFORMATION
Typically, you can use the Class Wizard to wrap all the functions in a type library. However, the Class Wizard is unable to wrap a function that requires an argument that is type SAFEARRAY and will generate a message like the following in the header file. // method 'MyMethod' not emitted because of invalid return type or   parameter type When you use Class Wizard to generate the wrapper classes for Microsoft PowerPoint, the Application::Run function will not be wrapped because it takes an argument of type SAFEARRAY. You can still call Application::Run by using the InvokeHelper function with the DISPID of 0x7e6. For details on the Run function, you can inspect the PowerPoint type library with the OLE/COM Viewer tool.

The following steps illustrate how you can automate PowerPoint to call Application::Run and run a PowerPoint macro.

Steps to create the PowerPoint presentation with macros
 Start Microsoft PowerPoint and create a new presentation with three blank slides. Press ALT+F11 to start the Visual Basic Editor. From the Insert menu, click Module to insert a module in the new presentation.  Add the following code to the module. 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  Save the presentation as one of the following, and then close PowerPoint:  In PowerPoint 2007, save the presentation as a Macro-enabled Presentation (*.pptm) by using a file name such as C:\Pres.pptm. In PowerPoint 2003 and in earlier versions of PowerPoint, save the presentation as a Presentation (*.ppt) by using a file name such as C:\Pres.ppt.</li></ul> </li></ol>

Steps to create the MFC application
<ol> Create a new MFC dialog box based application named AutoPPT .</li> From the View menu (or press CTRL+W), select the Class wizard, and then follow these steps: <ol style="list-style-type: lower-alpha;"> Click the Automation tab.</li> Click Add Class, and then click From Type Library.</li> Locate the PowerPoint library, and then add the PowerPoint Type Library: <ul> For PowerPoint 2007, add Msppt.olb.</li> For PowerPoint 2003, add Msppt.olb.</li> For PowerPoint 2002, add Msppt.olb.</li> For PowerPoint 2000, add Msppt9.olb.</li> For PowerPoint 97, add Msppt8.olb.</li></ul> </li></ol> </li>  Select the dialog box with the resource ID IDD_AUTOPPT_DIALOG. Add a button to the dialog box, and then add the following code to the handler for that button. _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; }

Note If you wrapped the classes in the PowerPoint 2002 type library, add an additional argument (false) to the call for the Open method. </li>  Add the appropriate include statement for the Powerpoint header file. </li>  Add the following line of code to the beginning of the CAutoPPTApp::InitInstance function. AfxOleInit; </li> Compile and Run. Select the button you added to the dialog box to run the automation code. Once the code completes, PowerPoint will remain visible so that you can observe the changes to the presentation made by the macros.</li></ol>
 * 1) include "msppt8.h" // use msppt9.h for 2000 or msppt.h for 2002, 2003, and 2007

<div class="references_section">