Click here to Skip to main content
Click here to Skip to main content

Automating MS PowerPoint Presentations

By , 25 Oct 2007
 

Introduction

I have been working on automating MS Office applications since some time. I have seen that it is very easy to find information on automation of MS Word but it is difficult to find information on automation of other Office apps like PowerPoint, Excel.

So I decided to write one by myself and share the information with others.

Using the Code

Automation of PowerPoint is same as that for Word. Create a new project, go ahead and right click on References in Solution Explorer, and select Add Reference… When the Add Reference window comes up, select the COM tab. This will list all the component names which are available on your machine. Since we are going to use MS PowerPoint, we will scroll down until we find: Microsoft Power Point Object Library.

The following code will help youu understand the rest of the automation:

public string presentationExtract(string path)
{
    int slideNumber = 0;
    string heading = null;
    PowerPoint.Application PPTApp = null;
    PowerPoint.Presentation PPTPre = null;
    bool errorFlag = false;
    StringBuilder sb = new StringBuilder("");
    string wordArt = null;

    try
    {

        PPTApp = new PowerPoint.Application();
        PPTPre = PPTApp.Presentations.Open(path, Microsoft.Office.Core.MsoTriState.msoTrue, 
                 Microsoft.Office.Core.MsoTriState.msoTriStateMixed, 0);

        foreach (PowerPoint.Slide objSlide in PPTPre.Slides)
        {
            slideNumber = objSlide.SlideNumber;
            heading = "\r\n\r\n" + "                PRESENTATION SLIDE " + 
                      slideNumber + "                " + "\r\n\r\n";
            sb.Append(heading);

            foreach (PowerPoint.Shape objShape in objSlide.Shapes)
            {
                try
                {
                    wordArt = objShape.TextEffect.Text.ToString();
                    sb.Append("\r\n" + wordArt + "\r\n");
                }
                catch (System.Exception wordartException)
                {
                    string temp = wordartException.Message.ToString();
                }

                try
                {
                    sb.Append("\r\n" + objShape.TextFrame.TextRange.Text + "\r\n");
                }
                catch (System.Exception textException)
                {
                    string temp = textException.Message.ToString();
                }
            }
        }
    }
    catch (System.Exception error)
    {
        string temp = error.Message.ToString();
        errorFlag = true;
    }
    finally
    {
        PPTPre.Close();
        PPTApp.Quit();
    }      

    if (!errorFlag)
    {         
       return (newContent);
    }
    else
        return ("");
}

Points of Interest

I want to mention one thing here: I have used Office XP COM object and this can be used with MS Office 2003 and 2007.

Good luck guys...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Govardhana Reddy
Web Developer India
India India
Member
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.
 
I want to be one among the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.) if not the "BEST"
 
My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".
 
Apart from my technical stuff I love Long Drives, Computer Gaming, Sports, Bikes and much more to say.
 
Anyways long road ahead keep me accompanied...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUsing automation without knowing MS Office versionmemberKristian Regec29 Oct '07 - 22:56 
I think that your code will work only on computers with same MS Office version as you add to your project references. Am I right ?
Is it possible to use PowerPoint automation without knowing user MS Office version ?
 
Best Regards,
Kristian.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 25 Oct 2007
Article Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid