Automating Microsoft Office Applications


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: Tutorials, Microsoft Office


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...
            AfxMessageBox("Couldn't start PowerPoint.");
        else    // Make PowerPoint visible and display a message
            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
        CFileDialog FileDlg(TRUE,"PPT",NULL,
        // To get the selected file's path and name
        CString strFileName;
        strFileName = FileDlg.GetPathName();
            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())
  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();
  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();
  20. And similarly, to go to the last slide
  21.   void CPowerPntDlg::OnBtnLast()
        Presentation    = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View            = SlideShowWindow.GetView();
  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();
  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();


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.


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date