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



The information in this article applies to:

  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002
  • Microsoft PowerPoint 2000
  • Microsoft PowerPoint 97 for Windows
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Foundation Classes (MFC)

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

  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 type library and add all the available classes.

    PowerPoint Type Libraries:
       Version 97   -  msppt8.olb
       Version 2000 -  msppt9.olb
       Version 2002 -  msppt.olb
       Version 2003 -  msppt.olb
    					
  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();
       }
    
    						
    NOTE: If you wrapped the classes in the PowerPoint 2002 type library, add an additional argument (false) to the call for the Open method.
  4. Add the appropriate include statement for the Powerpoint header file:
    #include "msppt8.h" // use msppt9.h for 2000 or msppt.h for 2002 and 2003
    					
  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:

180616 How To Use MFC to Create and Show a PowerPoint Presentation


Modification Type:MajorLast Reviewed:11/19/2004
Keywords:kbAutomation kbhowto KB237554