The Wayback Machine - https://web.archive.org/web/20080120100756/http://www.codeguru.com:80/cpp/com-tech/activex/tutorials/article.php/c5569/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Rackmount LCD Monitor
Promotional Gifts
Promotional Products
Freelance Programmers
Boat Donations
Web Hosting
Domains for Business
Contemporary Art
Internet Security
Memory
Hurricane Shutters
Compare Prices
Car Donations
Promotional Items


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Access FREE Intel Whitepapers and Briefs at the Intel Resource Center
Whitepaper:
Reduce Maintenance Costs While Increasing Notebook Security

Whitepaper:
Improve Security and Control of Your Desktop PC

Mobility Whitepaper:
Increase Performance, Security with Intel Centrino Pro Processor Technology

Servers/Workstations Whitepaper:
Wireless Technologies and e-Learning--Bridging the Digital Divide

Servers/Workstations Whitepaper:
Which Is Right for You--Itanium 2 or x86 Architecture?

Clients Whitepaper:
Protect, Connect, and Improve Security for All of Your Business PCs

Virtualization eBook:
Using Virtualization to Change the Face of Business

Virtualization Whitepaper: IT Agility through Automated, Policy-based Virtual Infrastructure
Home >> Visual C++ / C++ >> COM-based Technologies >> ActiveX Programming >> Tutorials

Whitepaper: Migrating an Enterprise WAN from ATM to IP. Ensure your network can support new services & future traffic growth, & take advantage of low-cost bandwidth from telecom providers.

Automating Microsoft Office Applications
Rating:

V Girish (view profile)
August 19, 2003

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


(continued)




Software Engineering with Microsoft Visual Studio Team System
 
Get a free copy of Software Engineering with Microsoft Visual Studio Team System to get a broad and thoughtful view on how to approach software engineering from a value-up paradigm. »

 
The Microsoft Platform - Driving Your Business Forward with ALM
 
Driving Your Business Forward with Application Lifecycle Management (ALM). Read this whitepaper to find out how you can get started with and the business benefits that you can achieve with an ALM strategy. »

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.

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Get 25% off Verio's Virtual Private Server (VPS) plans, this is a limited time offer--Click here for details.
Award-Winning Managed Dedicated Servers. Expert 24x7x365 toll-free support, 100% Network Uptime Guarantee.
Intel Go Parallel Portal Article: Ruby's Concurrency Nightmare.
Free Book Offer: Software Engineering with Microsoft Visual Studio Team System. Click Here.
Research Brief: SAP and Oracle--Who's Ready For SMB? Sponsored by Oracle.


RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Add a command button to a slide and set it caption? - hacaonguyen (08/16/2005)
In C++ how to detect an Automatic Date in a powerpoint slide? - one_apple (03/14/2005)
I have a problem - Legacy CodeGuru (02/06/2004)
how to create an acticex control and insert it to .ppt - Legacy CodeGuru (12/02/2003)
a small question - Legacy CodeGuru (11/25/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)
FREE Virtualization Resources from Intel:
eBook:
Using Virtualization to Change the Face of Business
Brief:
Building a Real-World Model to Assess Virtualization Platforms
Whitepaper:
IT Agility through Automated, Policy-based Virtual Infrastructure


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Solutions
Whitepapers and eBooks
Microsoft Article: Introduction to Office Business Applications (OBA)
Symantec Whitepaper: A Unified, Proactive Approach to Endpoint Security
APC eBook: Putting the Green in IT
IPSWITCH Whitepaper: The 7 Habits of Highly Effective IT Administrators
Intel Whitepaper: Optimizing Applications with the Intel C++ and Fortran Compilers
Symantec Whitepaper: Emerging Trends in Fighting Spam
Oracle eBook: Guide to Oracle 11g and Database Migration
Siemens Whitepaper:Demystifying Enterprise Fixed Mobile Convergence
Analyst Report: Assessing Your IT Organization
Avaya Whitepaper: SIP--Creating Next-Generation Telecom Applications
MessageLabs Whitepaper: Spam Spikes--A Real Risk to Your Business
Symantec Article: Guarding the Corporate Gateway
Webcasts
Microsoft: NXT Web Seminar Series
Mazu: Using NBA and ITIL's Service Asset and Configuration Management to Improve Management Information
Microsoft Partner Program Video: The Secrets to Partner Success
Rational Asset Manager: Succeed with Asset-based Development
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
Downloads and eKits
IBM Rational Systems Development Solution e-Kit
IBM SOA Development Survival Guide eKit
Evaluate IBM Rational Build Forge Online
IBM Enterprise Architect eKit for SOA
Evaluate Rational Application Developer Online
Iron Speed Designer Application Generator
Symantec IM Detection Utility
Tutorials and Demos
IBM Tutorial: Intro to XML User Interface Language (XUL) Development
Microsoft How-to Article: Get Going with Silverlight and Windows Live
IBM Tutorial: The Ajax Transport Method
IBM Tutorial: Learning PHP
IBM Tutorial: Validating XML