HOWTO: Automate PowerPoint Using MFC and Run a Macro

ID: Q237554


The information in this article applies to:
  • Microsoft PowerPoint versions 2000, 97 For Windows
  • Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0


SUMMARY

This article describes how you can use Microsoft Foundation Classes (MFC) to automate Microsoft PowerPoint and run a macro 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 (MSPPT8.olb or MSPPT9.olb) 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

  1. Start Microsoft PowerPoint and create a new presentation with three blank slides.


  2. Press ALT+F11 to start the Visual Basic Editor.


  3. From the Insert menu, click Module to insert a module in the new presentation.


  4. 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 


  5. Save the presentation as c:\pres.ppt and close PowerPoint.


Steps to Create the MFC Application

  1. Create a new MFC dialog box based application named AutoPPT.


  2. From the View menu (or press CTRL+W) select the Classwizard. Select the Automation tab. Click Add Class and select From Type Library. Locate the PowerPoint 97 type library MSPPT8.olb (or the PowerPoint 2000 type library MSPPT9.olb) and add all the available classes.


  3. Select the dialog box with the resource ID IDD_AUTOPPT_DIALOG. Add a button to the dialog box and 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;
       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();
       }
     


  4. Add the following line to the top of AutoPPTDlg.cpp for PowerPoint 97:
    
       #include "msppt8.h" 
    or add the following line to the top of AutoPPTDlg.cpp for PowerPoint 2000:
    
       #include "msppt9.h" 


  5. Add the following line of code to the beginning of the CAutoPPTApp::InitInstance() function:
    
         AfxOleInit(); 


  6. 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.



REFERENCES

For additional information about automating PowerPoint using Visual C++, please see the following article in the Microsoft Knowledge Base:

Q180616 HOWTO: Use MFC to Create and Show a PowerPoint Presentation
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Lori Turner, Microsoft Corporation.

Additional query words:

Keywords : kbAutomation kbMFC kbVC kbVC500 kbVC600 kbGrpDSO kbDSupport kbpowerpt2000
Version : WINDOWS:2000,97; winnt:5.0,6.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: August 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.