Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 8. August 2010 17:30

I am currently on a project which involves creating a PowerPoint VSTO add-in. I have very limited experience with PowerPoint automation, so before committing to the project, I thought it would be a good idea to explore a bit the object model, to gauge how difficult things could get, and I set to write a small PowerPoint add-in which would automatically translate slides. Sounds like a simple enough project, how difficult could it be?

Turns out, not too difficult, but not completely trivial either. I discovered quickly that the PowerPoint object model, unlike most Office applications, doesn’t have much (any?) documentation for the .Net developer; the best I found is the VBA PowerPoint 2007 developer reference, which gives a decent starting point to figure out what the objects are about. So I thought I would share my exploration of the PowerPoint jungle, and hopefully spare some trouble to other .Net developers.

The plan

The objective is simple: write an add-in which allows the user to

  • select a language to translate from, and a language to translate to,
  • create a duplicate of the current slide, translating all the text and keeping the layout

The plan will be to use Google Translate to perform the translation. In order to do that, we will nedd to extract out all pieces of text that require translating.

Finding all the text in a slide

Lets’ start by identifying where we have text in the current slide. Let’s first create a PowerPoint 2007 Add-in project in Visual Studio. To keep things simple for now, we will add a Ribbon control with a button, and when that button is clicked, we’ll start working on the current slide:

RibbonWithButton

Double-click on the Button (I renamed my button translateButton) to generate an event handler for the Click event, and get the current Slide:

private void translateButton_Click(object sender, RibbonControlEventArgs e)
{
   var powerpoint = Globals.ThisAddIn.Application;
   if (powerpoint.ActivePresentation.Slides.Count > 0)
   {
      var slide = (PowerPoint.Slide)powerpoint.ActiveWindow.View.Slide as PowerPoint.Slide;
   }
}

Using the static class Globals, I access the PowerPoint application. First, I check that the active presentation has a slide (Unlike Excel for instance, where a Workbook must contain at least a Worksheet, PowerPoint can have a Presentation with no slide), and then I navigate to the Slide that is currently active in the ActiveWindow.View. Note that View.Slide returns an object, so we need to explicitly cast it to a Slide.

Now that we have a Slide in our hands, we need to search for text in it. The visible items on a slide are Shapes. Let’s add the following code, and run it:

foreach (var item in slide.Shapes)
{
   var shape = (PowerPoint.Shape)item;
   MessageBox.Show(shape.Name);
}

Interestingly, if you run this on an empty, default slide, you will see names like “Title 1” and “Content Placeholder 2” pop up in the message box. Even the default placeholders are Shapes.

Out of all these Shapes, which could be anything, from a video to a geometric shape, we need to find the ones which contain text. To do this, we will use Shape.HasTextFrame, a property which indicates whether the Shape can contain text. Naively, I expected this property to be a boolean – but that would be too simple. It’s actually an enum, Microsoft.Office.Core.MsoTriState, which, fascinatingly, has 5 possible values: msoTrue, msoFalse (so far so good), msoCTrue, msoTriStateMixed, and msoTriStateToggle. I am still not quite clear what the three last ones are, but I’ll happily ignore that for now.

HasTextFrame means that the Shape could contain text, and not that it does. Let’s check whether the shape has text, and finally, after unwrapping all these layers, we can access the Text of the Shape, through its TextRange property:

if (shape.HasTextFrame == MsoTriState.msoTrue)
{
  if (shape.TextFrame.HasText == MsoTriState.msoTrue)
  {
     var textRange = shape.TextFrame.TextRange;
     var text = textRange.Text;
     MessageBox.Show(text);
  }
}

If you run the add-in on a Slide like this one, you should see that it successfully finds the 3 pieces of text, in the title, the content, and in the black square.

SlideWithText

However, if you dig in a bit in the Text from the main content, you’ll see that the actual string looks something like this:

Level 1 Bullet 1\rLevel 2 Bullet 1\rLevel 2 Bullet 2\rLevel 1 Bullet 2

Besides our text, it also contains \r, the character for Carriage Return, which indicates the end of a paragraph. This means that if we want to preserve the layout of the slide, we’ll have to pay attention not to mess with the line breaks when manipulating the Text. In our next installment, we’ll look into that, and explore the TextRange property.

Comments

8/21/2010 4:38:42 PM #

trackback

Exploring the PowerPoint TextRange object

Exploring the PowerPoint TextRange object

Clear Lines Blog | Reply

9/5/2011 7:55:17 AM #

trackback

Exploring the PowerPoint TextRange object

Exploring the PowerPoint TextRange object

Clear Lines Blog | Reply

12/18/2011 8:40:37 PM #

zain

thanks bro. it has solved my problem too.


zain from
PAKISTAN...

zain United States | Reply

12/20/2011 4:27:30 PM #

Problem

Hi,
What if slide contains grouped shapes. That time .HasText  will not help right?

Problem United States | Reply

7/7/2012 11:28:18 AM #

pingback

Pingback from entertainmentweeklymagazine.wfiedler-free-templates.org

Traditional Wedding Anniversary Gifts | Internet Contents | Entertainment Weekly Magazine

entertainmentweeklymagazine.wfiedler-free-templates.org | Reply

6/6/2013 6:50:19 AM #

Heather

This was so helpful.  Many thanks!!

Heather Canada | Reply

8/8/2013 5:51:43 PM #

pingback

Pingback from com-lab.biz

Accessing Slide object in PowerPoint add-in | user96

com-lab.biz | Reply

11/10/2016 3:21:18 AM #

pingback

Pingback from jame17.voajoyka.xyz

How To Fit Text To Slide In Powerpoint | Information

jame17.voajoyka.xyz | Reply

12/17/2016 1:49:00 AM #

pingback

Pingback from steroidsforsale.biz

where can i buy real dbol

steroidsforsale.biz | Reply

12/19/2016 11:16:47 PM #

pingback

Pingback from steroidsforsale.biz

bodybuilding steroids for sale

steroidsforsale.biz | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Comments

Comment RSS