Click here to Skip to main content
11,702,130 members (62,156 online)
Click here to Skip to main content

Automating MS-Office applications

, 29 Jun 2002 181.1K 93
Rate this:
Please Sign up or sign in to vote.
This tutorial helps you to automate PowerPoint, with hints to automate other MS-Office applications

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

Steps to follow

By following the same steps given below, you can automate , word, excel or any 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 class-wizard , select the automation tab, select "Add Class" ... "From a type library" and 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 to go to the next slide now ? In that case, this function will help you.
    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 etc. and you can go ahead on your own. This is the basic framework and you can see how easy it is to handle PowerPoint.  Its the same case with excel, word or any other Microsoft office application. All luck 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

VGirish
Founder
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralCCmdTarget::FromIDispatch Pin
cotton541525-Sep-10 0:28
membercotton541525-Sep-10 0:28 
QuestionCan you provide source code,thanks Pin
JoshuaTsai6-Dec-09 23:15
memberJoshuaTsai6-Dec-09 23:15 
Generalwhere is ppt8.h Pin
fatterbetter6-Oct-08 21:58
memberfatterbetter6-Oct-08 21:58 
GeneralDynamic XML template-based PowerPointCreator in C# Pin
Elmue29-Aug-08 13:04
memberElmue29-Aug-08 13:04 
Questionhow to play .ppt file in our own view? Pin
wlhuang15-Jun-08 17:41
memberwlhuang15-Jun-08 17:41 
GeneralReset auto timing Pin
niloy12-Jun-08 2:23
memberniloy12-Jun-08 2:23 
Questionhow set active printer using power point Automation Pin
nabeel811-Sep-07 23:21
membernabeel811-Sep-07 23:21 
QuestionHi All, I have to write a code in VC++ , to convert *.ppt file to *.jpg file,can you tell me how can i do it????? Pin
SKhokalay2-Nov-06 2:06
memberSKhokalay2-Nov-06 2:06 
Generalturning on multiple monitors support in powerpoint Pin
Manoj Singh K24-Oct-06 23:22
memberManoj Singh K24-Oct-06 23:22 
Questionhow to convert powerpoint office 97 to images slide show Pin
Dinh Long8-Jun-06 22:55
memberDinh Long8-Jun-06 22:55 
Questionquestion? Pin
shoulderlll27-Jan-05 20:41
membershoulderlll27-Jan-05 20:41 
QuestionWhere is msppt8.h, and are there any general docs? Pin
Nightfox12-Dec-04 20:40
memberNightfox12-Dec-04 20:40 
AnswerRe: Where is msppt8.h, and are there any general docs? Pin
hawkshim14-Jun-07 15:44
memberhawkshim14-Jun-07 15:44 
QuestionHow do I get PowerPoint to call my app? Pin
sprezzatura30-Nov-04 10:53
membersprezzatura30-Nov-04 10:53 
QuestionHelp me !!! How to get data from MS office documents? Pin
hoan_vacnew13-Jul-04 15:37
memberhoan_vacnew13-Jul-04 15:37 
AnswerRe: Help me !!! How to get data from MS office documents? Pin
vishalmore19-Nov-04 22:30
membervishalmore19-Nov-04 22:30 
GeneralPerfect!! But, Outlook example Pin
d00_ape1-Jul-04 22:24
memberd00_ape1-Jul-04 22:24 
GeneralHelp Pin
maxmach14-May-04 2:57
membermaxmach14-May-04 2:57 
Generallong delay in createdispatch Pin
rajas20-Mar-04 11:15
memberrajas20-Mar-04 11:15 
Questioncannot find msppt8.h?? Pin
xxhimanshu24-Feb-04 0:26
sussxxhimanshu24-Feb-04 0:26 
GeneralHi, Pin
Neha19-Dec-03 21:43
memberNeha19-Dec-03 21:43 
GeneralUsing Word as E-mail Editor for letters Pin
Danis Soloviev23-Nov-03 23:45
sussDanis Soloviev23-Nov-03 23:45 
Questionhow to get the notes content of a presentation Pin
bernadin26-Sep-03 19:25
memberbernadin26-Sep-03 19:25 
GeneralRedirect the SlideShow output in PowerPoint Pin
Shane.B20-Aug-03 0:25
memberShane.B20-Aug-03 0:25 
Generalread and write MS Office file with out open it Pin
RomanticHero31-Jul-03 5:09
memberRomanticHero31-Jul-03 5:09 
GeneralRe: read and write MS Office file with out open it Pin
Jisys DevTeam12-Aug-03 12:40
memberJisys DevTeam12-Aug-03 12:40 
GeneralRe: read and write MS Office file with out open it Pin
Anonymous27-Aug-03 2:30
sussAnonymous27-Aug-03 2:30 
GeneralPb "Flash App Power" with my Api Pin
hermine45-May-03 4:43
memberhermine45-May-03 4:43 
Question"Automation tab" in .NET? Pin
JockeP3-Apr-03 13:10
memberJockeP3-Apr-03 13:10 
AnswerRe: "Automation tab" in .NET? Pin
DaveyWavey8-Apr-03 8:11
memberDaveyWavey8-Apr-03 8:11 
GeneralRe: "Automation tab" in .NET? Pin
Kevin Gutteridge16-Dec-04 21:43
memberKevin Gutteridge16-Dec-04 21:43 
GeneralVersion??? PowerPoint 97,2000,XP .... Pin
David Hezky30-Sep-02 5:15
sussDavid Hezky30-Sep-02 5:15 
GeneralRe: Version??? PowerPoint 97,2000,XP .... Pin
Dinh Long8-Jun-06 23:13
memberDinh Long8-Jun-06 23:13 
QuestionHow to play ppt into a MFC view Pin
Wu Gang13-Aug-02 5:24
memberWu Gang13-Aug-02 5:24 
AnswerRe: How to play ppt into a MFC view Pin
bernadin26-Sep-03 19:22
memberbernadin26-Sep-03 19:22 
GeneralRe: How to play ppt into a MFC view Pin
dexy-17-Nov-05 2:21
memberdexy-17-Nov-05 2:21 
GeneralRe: How to play ppt into a MFC view Pin
asanka_itfac16-Mar-10 22:26
memberasanka_itfac16-Mar-10 22:26 
GeneralQuestion Pin
Michael Kuelshammer29-Jun-02 6:14
memberMichael Kuelshammer29-Jun-02 6:14 
GeneralRe: Question Pin
Tony Fontenot29-Jun-02 6:59
memberTony Fontenot29-Jun-02 6:59 
GeneralRe: Question Pin
Magnus3-Dec-03 12:33
memberMagnus3-Dec-03 12:33 
GeneralRe: Question Pin
vishalmore4-Aug-04 21:41
membervishalmore4-Aug-04 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 30 Jun 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid