Automating Microsoft Office Applications

Environment: Tutorials, Microsoft Office

Introduction

This tutorial helps you to learn the basics of automation. With this code, you can control PowerPoint from your application. You can open PowerPoint programmatically, open any presentation, go to any slide that you want to, run the slideshow, and so forth.

Steps to Follow

By following the same steps given below, you can automate Word, Excel, or any other Microsoft Office application.

  1. Create a dialog-based application and in the App Wizard's step 3 of 6, select the automation checkbox.
  2. Create buttons for Start, Run, Close, First Slide, Last Slide, Previous Slide, and Next Slide functions and use the following functions accordingly.
  3. In your application's InitInstance function, add the following lines:
  4.   // Initialize OLE libraries
      if (!AfxOleInit())
      {
        AfxMessageBox("Failed to initialize OLE");
        return FALSE;
      }
    
  5. In your dialog's class, open the class wizard, select the automation tab, select "Add Class" ... "From a type library", and then select msppt8.olb from "C:\Program Files\Microsoft Office\Office\".
  6. In your header file of your dialog, include the following line:
  7.   #include "msppt8.h"
  8. Add the following variables in your dialog's header file.
  9.   _Application app;    // app is the PowerPoint
                           // _Application object
    
      Presentations Presentations;
      _Presentation Presentation;
    
      SlideShowView View;
    
      SlideShowWindow SlideShowWindow;
      SlideShowSettings slideshow;
      Slides slides; 
      _Slide slide;
    
  10. To start PowerPoint, you have to write this code in the Start button's function.
  11.   void CPowerPntDlg::OnBtnStart()
      {
        // Start PowerPoint and get Application object...
        if(!app.CreateDispatch("Powerpoint.Application"))
        {
            AfxMessageBox("Couldn't start PowerPoint.");
        }
        else    // Make PowerPoint visible and display a message
        {
            app.SetVisible(TRUE);
            TRACE("PowerPoint is Running!");
        }
      }
    
  12. To open a presentation from the hard disk, add this code in the Open button's function call.
  13.   void CPowerPntDlg::OnBtnOpen()
      {
        static char BASED_CODE szFilter[] = "PowerPoint Files
                                            (*.ppt)|*.ppt||";
        CFileDialog FileDlg(TRUE,"PPT",NULL,
                            OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON|
                            OFN_PATHMUSTEXIST,szFilter);
        FileDlg.DoModal();
    
        // To get the selected file's path and name
        CString strFileName;
        strFileName = FileDlg.GetPathName();
    
        if(!strFileName.IsEmpty())
        {
            Presentations = app.GetPresentations();
            Presentation  = Presentations.Open(strFileName,0,0,1);
        }
      }
    
  14. To close PowerPoint, add this code in the Close button's function call.
  15.   void CPowerPntDlg::OnBtnClose()
      {
        if (CanExit())
            app.Quit();
      }
    
  16. To run the slideshow, use this code in the Run button's function call
  17.   void CPowerPntDlg::OnBtnRun()
      {
        Presentations = app.GetActivePresentation();
          slides      = Presentation.GetSlides();
         // Show the first slide of the presentation
        slide         = slides.Item(COleVariant((long)1));
    
        //Run the show
        slideshow     = Presentation.GetSlideShowSettings();
        slideshow.Run();
      }
    
  18. Sometimes, you might want to start all over from the first slide. To go to the first slide, you can use this code:
  19.   void CPowerPntDlg::OnBtnFirst()
      {
        Presentation    = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View            = SlideShowWindow.GetView();
        View.First();
      }
    
  20. And similarly, to go to the last slide
  21.   void CPowerPntDlg::OnBtnLast()
      {
        Presentation    = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View            = SlideShowWindow.GetView();
        View.Last();
      }
    
  22. Now that you have the slideshow running, you would obviously want to go to the previous slide at some point of time. To do just that, you can use this code:
  23.   void CPowerPntDlg::OnBtnPrevious()
      {
        Presentation    = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View            = SlideShowWindow.GetView();
        View.Previous();
      }
    
  24. Interested in going to the next slide now? In that case, this function will help you:
  25.   void CPowerPntDlg::OnBtnNext()
      {
        Presentation    = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View            = SlideShowWindow.GetView();
        View.Next();
      }
    

Conclusion

That's it, folks. Check out the other functions available for transitions, animations, and so forth and you can go ahead on your own. This is the basic framework and you can see how easy it is to handle PowerPoint. It's the same case with Excel, Word, or any other Microsoft Office application. All luck to you and have a great time. You can also check out http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749 for more information.

I used this code to do remote PowerPoint presentations. The concept was to run a presentation at one place and the clients will be looking at it simultaneously from different places at the same time.



Comments

  • Add a command button to a slide and set it caption?

    Posted by hacaonguyen on 08/16/2005 05:46am

    Dear,
    I added a command button to a slide as follow:
    ////////////////////
    //MyCode Begin
    oShape = oShapes.AddOLEObject(10, 10, 100, 50, "Forms.CommandButton.1", "", 0, "", 0, "", );
    oShape.SetName("cmdRun");
    //MyCode End
    /////////////////////////
    But my broplem is I could not set caption and VB code for this button.
    I can do this in VB as below code:
    '''''''''''''''''''''''''
    'MyCode in VB Begin
    Set oShape = oSlide.Shapes.AddOLEObject(200, 200, 100, 0, "Forms.CommandButton.1")
    oShape.OLEFormat.Object = True
    oShape.OLEFormat.Object.Name = "cmdRun" 'or oShape.Name = "cmdRun"
    oShape.OLEFormat.Object.Caption = "Run"
    Dim sCode As String
    sCode = "Private Sub " &
    oShape.OLEFormat.Object.Name & "_Click()" &
    vbCrLf & _
    " MsgBox ""You Clicked the
    CommandButton""" & vbCrLf & _
    "End Sub"
    oPresent.VBProject.VBComponents
    (1).CodeModule.AddFromString sCode
    'MyCode in VB End
    ''''''''''''''''''''''''
    Could any one help.
    Many thanks

    Reply
  • In C++ how to detect an Automatic Date in a powerpoint slide?

    Posted by one_apple on 03/14/2005 02:26pm

    I will appreciate anybody's help on this: In C++ how to detect an Automatic Date in a powerpoint slide? Thanks

    Reply
  • I have a problem

    Posted by Legacy on 02/06/2004 12:00am

    Originally posted by: Jenny

    Very cool your explanation, but nowdays we have another problem.
    U know all the Sun-Microsoft problem....
    From september on there will be no support anymore for the Java packages Microsoft created and that includes the ones used to automate Office. Please, tell me! How would be possible to automate it without using COM stuff, I mean without using the packages Microsoft created? Please, help me! I've been wondering around with no practical solution and u seem very practical!
    Thanx on advance!

    Reply
  • how to create an acticex control and insert it to .ppt

    Posted by Legacy on 12/02/2003 12:00am

    Originally posted by: david


    i create my activex control,

    i use it in .htm correctly.

    now , i want to use my activex control in a .ppt file

    how can i do for this?

    please help me

    thanks

    david

    Reply
  • a small question

    Posted by Legacy on 11/25/2003 12:00am

    Originally posted by: padma

    the article written by you is really good n helpful,but right now im interested to kno is .. is it possible to have it the other way round i.e. controlling an application(for e.g. an applet) through microsoft powerpoint??? plzzzzzzz do lemme kno abt the possibilities,if any..and the way to go about it. informaion provided on this matter will be greatly appreciated.
    

    Reply
  • I Compliment You On Sharing

    Posted by Legacy on 08/26/2003 12:00am

    Originally posted by: Dave Busey

    Thank you for setting out such a nice tutorial. The code and explanations are going to save some of us a lot of time from reference searching and experimentation. I appreciate your generosity.

    Dave Busey

    Reply
  • Great pice of work.

    Posted by Legacy on 08/22/2003 12:00am

    Originally posted by: Ilya Kalujny

    Could be really useful sometimes.

    Reply
  • Cool, but is it possible to remote to another machine?

    Posted by Legacy on 08/20/2003 12:00am

    Originally posted by: Jackson Lee

    Your code works on the same machine, is it possible CreateDispatch on another machine?

    thx.

    lee

    Reply
  • Good info But.....

    Posted by Legacy on 08/19/2003 12:00am

    Originally posted by: one4all

    Good Article, but, I didn't understand why did you need this application for "Remote power presentation".
    You could have peacefully used Netmeeting right?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds