Need sample PowerPoint VBA code

J

Joseph M. Newcomer

I've discovered the VBA documentation is moderately useless; it seems to concentrate on
modifying documents, and simple things do not appear anywhere.

I have a very simple requirement. I need to iterate over every slide of a PowerPoint
presentation, find every word on every slide, and write the words out to a file with the
word and its slide #, e.g., if a slide says

THIS IS AN EXAMPLE

* This is a sample slide
* It has several bulleted points

I want to get, if this were slide 22 of the presentation,

THIS 22
IS 22
AN 22
EXAMPLE 22
This 22
is 22
a 22
sample 22
slide 22
It 22
has 22
several 22
bulleted 22
points 22

I want to find every word in every text box on every slide and write it out as shown.

I can't even find how to do this; every example deals with useless things like changing
slide backgrounds, or adding slides, or other things of no interest to me. But the
simplest concept, iterating over every slide, is not at all obvious, and the next level,
iterating over every window in the slide, is not at all obvious, and the next level,
iterating over every word, is probably simple, since that's simple programming of a loop
to look for whitespace, but getting to the text appears not at all obvious.

Any hints, examples, links, etc. would be appreciated.

I've figured out how to open a file in VBA (trivial) and write text to it (trivial), but
the structure of the document and iterators are not making themselves known.
joe
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
A

Andy Pope

Hi,

Here is a starting point. It should capture textbox and table text.

Sub XX()
Dim sldTemp As Slide
Dim shpTemp As Shape
Dim vntWord As Variant
Dim lngRow As Long
Dim lngCol As Long

For Each sldTemp In ActivePresentation.Slides
For Each shpTemp In sldTemp.Shapes
If shpTemp.HasTextFrame Then
For Each vntWord In shpTemp.TextFrame.TextRange.Words
Debug.Print vntWord, sldTemp.SlideIndex
Next
ElseIf shpTemp.HasTable Then
With shpTemp.Table
For lngRow = 1 To .Rows.Count
For lngCol = 1 To .Columns.Count
If .Cell(lngRow, lngCol).Shape.HasTextFrame Then
For Each vntWord In .Cell(lngRow,
lngCol).Shape.TextFrame.TextRange.Words
Debug.Print vntWord, sldTemp.SlideIndex
Next
End If
Next
Next
End With
End If
Next
Next

End Sub

Cheers
Andy
 
S

Steve Rindsberg

Very nicely done. The one thing I'd change is:

For Each vntWord In shpTemp.TextFrame.TextRange.Words
' THIS
On Error Resume Next
Debug.Print vntWord, sldTemp.SlideIndex
On Error GoTo 0 ' or a normal error handler
Next

Same thing in the portion where you're pulling text from table cells.

Some versions of PPT allow a condition where a shape has a text frame but PPT
throws an error if you try to access the text in the shape.

Joseph, you might also consider performing this whole operation on a temp
version of your real PPT file. That way you could also ungroup all group
shapes and OLE objects, which'd let you at the text in groups and in charts,
etc.
 
J

Joseph M. Newcomer

Thanks, all. That's what I needed!
joe
I've discovered the VBA documentation is moderately useless; it seems to concentrate on
modifying documents, and simple things do not appear anywhere.

I have a very simple requirement. I need to iterate over every slide of a PowerPoint
presentation, find every word on every slide, and write the words out to a file with the
word and its slide #, e.g., if a slide says

THIS IS AN EXAMPLE

* This is a sample slide
* It has several bulleted points

I want to get, if this were slide 22 of the presentation,

THIS 22
IS 22
AN 22
EXAMPLE 22
This 22
is 22
a 22
sample 22
slide 22
It 22
has 22
several 22
bulleted 22
points 22

I want to find every word in every text box on every slide and write it out as shown.

I can't even find how to do this; every example deals with useless things like changing
slide backgrounds, or adding slides, or other things of no interest to me. But the
simplest concept, iterating over every slide, is not at all obvious, and the next level,
iterating over every window in the slide, is not at all obvious, and the next level,
iterating over every word, is probably simple, since that's simple programming of a loop
to look for whitespace, but getting to the text appears not at all obvious.

Any hints, examples, links, etc. would be appreciated.

I've figured out how to open a file in VBA (trivial) and write text to it (trivial), but
the structure of the document and iterators are not making themselves known.
joe
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
J

Joseph M. Newcomer

There seems to be a serious bug in PPT2007. I read the file into PPT2007, and wrote it
back out in what is called "compatibility" mode, but the problem was that it seems to have
"promoted" all the text boxes on the master slide so that they are now seen in PPT2003 as
being first-class text boxes in the presentation. Anyone know how to detect if a box is,
or formerly was, a master slide text box?

Also, I'd like to get the output in Unicode.
joe

Thanks, all. That's what I needed!
joe

Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
D

Dave Jenkins

Steve Rindsberg said:
Very nicely done.
[snip]

Joseph, you might also consider performing this whole operation on a temp
version of your real PPT file. That way you could also ungroup all group
shapes and OLE objects, which'd let you at the text in groups and in charts,
etc.

Hi Steve: I'm late to this party, but I just stumbled across your post here
while researching an allied issue. There's really no need to work on a temp
version of the file to in order to get at grouped text - a simple recursive
routine will let you dig right down into the grouped shapes.

Sub doWhatYouGottaDo()

Dim i As Integer
Dim j As Integer

For i = 1 To ActivePresentation.Slides.Count
For j = 1 To ActivePresentation.Slides(i).Shapes.Count
' recurse through shapes until there's one that's not grouped
recurseShapes ActivePresentation.Slides(i).Shapes(j)
Next j
Next i

End Sub


' recursive routine to drill down to lowest non-grouped shape in order to
tease out text
Sub recurseShapes(sh As Shape)

Dim i As Integer

If sh.Type = msoGroup Then
For i = 1 To sh.GroupItems.Count
recurseShapes sh.GroupItems(i)
Next i
Else
If sh.HasTextFrame = msoTrue Then

' do something with the text from that text frame
(sh.TextFrame.TextRange.text)

End If

End If

End Sub
 
S

Steve Rindsberg

Hi Dave,

A recursive search will indeed allow you to get at the text in any PPT-created
graphics, but not OLE objects (MSGraph charts, Excel content, etc) or some
inserted pictures. For that, you really do need to ungroup them.

While it's possible to reconstruct the groups after you're done, you can't put
Humpty together again if he's an ungrouped OLE object. On the other hand, if
you're working on a temporary copy of the original file, you can use Humpty to
make omelettes. No harm done. <g>

Dave Jenkins said:
Steve Rindsberg said:
Very nicely done.
[snip]

Joseph, you might also consider performing this whole operation on a temp
version of your real PPT file. That way you could also ungroup all group
shapes and OLE objects, which'd let you at the text in groups and in charts,
etc.

Hi Steve: I'm late to this party, but I just stumbled across your post here
while researching an allied issue. There's really no need to work on a temp
version of the file to in order to get at grouped text - a simple recursive
routine will let you dig right down into the grouped shapes.

Sub doWhatYouGottaDo()

Dim i As Integer
Dim j As Integer

For i = 1 To ActivePresentation.Slides.Count
For j = 1 To ActivePresentation.Slides(i).Shapes.Count
' recurse through shapes until there's one that's not grouped
recurseShapes ActivePresentation.Slides(i).Shapes(j)
Next j
Next i

End Sub

' recursive routine to drill down to lowest non-grouped shape in order to
tease out text
Sub recurseShapes(sh As Shape)

Dim i As Integer

If sh.Type = msoGroup Then
For i = 1 To sh.GroupItems.Count
recurseShapes sh.GroupItems(i)
Next i
Else
If sh.HasTextFrame = msoTrue Then

' do something with the text from that text frame
(sh.TextFrame.TextRange.text)

End If

End If

End Sub
 
S

Steve Rindsberg

OBTW:
Dim i As Integer
Dim j As Integer

For i = 1 To ActivePresentation.Slides.Count
For j = 1 To ActivePresentation.Slides(i).Shapes.Count

Better to dim the vars as Long, since Slides.Count and Shapes.Count return long.
Sooner or later, mix'n'match will bite you, and my betters tell me that Longs are
actually faster in 32-bit environments (the integers get converted to longs in
actual use; going with a long from the getgo saves that step)
 
D

Dave Jenkins

OT:

Good points.

I never have to deal with those kinds of embedded things, so they have not
[yet] been a consideration. Got a sample of code that can be used to
reconstruct grouped things? If I were to be working on a file that had an
embedded OLE object, and ungrouped it:

1. Are there any filesize implications to storing that data ungrouped?
2. Having ungrouped the stuff, will there be any changes to the appearance
of the data? That is, will PPT take it upon him/herself to shove stuff
around when it's been ungrouped?
3. Any other gotchas?

I'm asking this stuff, because I may just want to go ahead and ungroup
regardless, and carry the ungrouped stuff throught the rest of the life-cycle
of that presentation.

Thanks
--
Dave Jenkins
K5KX


Steve Rindsberg said:
Hi Dave,

A recursive search will indeed allow you to get at the text in any PPT-created
graphics, but not OLE objects (MSGraph charts, Excel content, etc) or some
inserted pictures. For that, you really do need to ungroup them.

While it's possible to reconstruct the groups after you're done, you can't put
Humpty together again if he's an ungrouped OLE object. On the other hand, if
you're working on a temporary copy of the original file, you can use Humpty to
make omelettes. No harm done. <g>

Dave Jenkins said:
Steve Rindsberg said:
Very nicely done.
[snip]

Joseph, you might also consider performing this whole operation on a temp
version of your real PPT file. That way you could also ungroup all group
shapes and OLE objects, which'd let you at the text in groups and in charts,
etc.

Hi Steve: I'm late to this party, but I just stumbled across your post here
while researching an allied issue. There's really no need to work on a temp
version of the file to in order to get at grouped text - a simple recursive
routine will let you dig right down into the grouped shapes.

Sub doWhatYouGottaDo()

Dim i As Integer
Dim j As Integer

For i = 1 To ActivePresentation.Slides.Count
For j = 1 To ActivePresentation.Slides(i).Shapes.Count
' recurse through shapes until there's one that's not grouped
recurseShapes ActivePresentation.Slides(i).Shapes(j)
Next j
Next i

End Sub

' recursive routine to drill down to lowest non-grouped shape in order to
tease out text
Sub recurseShapes(sh As Shape)

Dim i As Integer

If sh.Type = msoGroup Then
For i = 1 To sh.GroupItems.Count
recurseShapes sh.GroupItems(i)
Next i
Else
If sh.HasTextFrame = msoTrue Then

' do something with the text from that text frame
(sh.TextFrame.TextRange.text)

End If

End If

End Sub

--
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================
 
S

Steve Rindsberg

I never have to deal with those kinds of embedded things, so they have not
[yet] been a consideration. Got a sample of code that can be used to
reconstruct grouped things?

Nothing I could turn loose of w/o possibly running afoul of a client's
interests, alas.
If I were to be working on a file that had an
embedded OLE object, and ungrouped it:

1. Are there any filesize implications to storing that data ungrouped?

"Implications" has such a negative connotation. <g>
Generally, ungrouping OLE objects will make your PPT files smaller.
The object consists of OLE stuff and a WMF picture. Ungrouping leaves just the
picture.
2. Having ungrouped the stuff, will there be any changes to the appearance
of the data? That is, will PPT take it upon him/herself to shove stuff
around when it's been ungrouped?

The only times I've seen that happen was when there were other problems with the
object. When I was in the service bureau biz, we could be sure that if
ungrouping caused motion, the OLE version wasn't going to print right anyhow.
3. Any other gotchas?

Of course, you can no longer doubleclick to edit the object ... it's just a
picture once ungrouped. I won't swear that there are no others. It's Windows.
It's Office. It's OLE. There's a couple strikes right there. said:
I'm asking this stuff, because I may just want to go ahead and ungroup
regardless, and carry the ungrouped stuff throught the rest of the life-cycle
of that presentation.

Ah. That last gotcha might be the killer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top