Click here to Skip to main content
15,891,993 members
Articles / Productivity Apps and Services / Microsoft Office

Automating MS PowerPoint Presentations

Rate me:
Please Sign up or sign in to vote.
2.10/5 (4 votes)
25 Oct 2007CPOL 23.2K   16   1
This article will demonstrate how to automate and get content from an MS PowerPoint presentation.

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:

C#
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)


Written By
Web Developer India
India India
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...

Comments and Discussions

 
QuestionUsing automation without knowing MS Office version Pin
Kristian Regec29-Oct-07 22:56
Kristian Regec29-Oct-07 22:56 

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

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