Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

Automating MS PowerPoint Presentations

2.10/5 (4 votes)
25 Oct 2007CPOL 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)