Odd VBA Replace behavior in PPT2007

J

John Svendsen

Hi All:
I use Shyam Pillai's GlobalFindAndReplace Macro (listed below) in PPT2000
and PPT2003 without a hitch. However, when I tried to run the macro in
PPT2007 that has a table, the most odd thing happens - I get an error 445
(object doesn't support this action) at line "Select Case oShp.Type" when
"ReplaceText" is called again with one of the table's cell as the shape.
I've tried this on 2 PCs, with different presentations (last time I simply
created a new presentation, added a 2x2 table and ran this macro - same
error)
Has anyone else seen this?
TIA, JS

------------------------------------------------------------------------------------
Sub GlobalFindAndReplace()
'Let's take a look at how to make use of the Replace method of the TextRange
object in PowerPoint to create a global find and replace routine which
replaces the text across all open presentations. Thanks to Joe Stern who
noted that I hadn't included code to support tables.
'Note: PowerPoint 2007 object model has broken this line - Do While Not
oTmpRng Is Nothing.
' --------------------------------------------------------------------------------
' Copyright ©1999-2007, Shyam Pillai, All Rights Reserved.
' --------------------------------------------------------------------------------
' You are free to use this code within your own applications, add-ins,
' documents etc but you are expressly forbidden from selling or
' otherwise distributing this source code without prior consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
' --------------------------------------------------------------------------------
Dim oPres As Presentation
Dim oSld As Slide
Dim oShp As Shape
Dim FindWhat As String
Dim ReplaceWith As String

FindWhat = "Hi"
ReplaceWith = "Hello"
'For Each oPres In Application.Presentations
For Each oSld In ActivePresentation.Slides 'For Each oSld In
oPres.Slides
For Each oShp In oSld.Shapes
Call ReplaceText(oShp, FindWhat, ReplaceWith)
Next oShp
Next oSld
'Next oPres

End Sub
Sub ReplaceText(oShp As Object, FindString As String, ReplaceString As
String)
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim I As Integer
Dim iRows As Integer
Dim iCols As Integer
Dim oShpTmp As Shape

' Always include the 'On error resume next' statememt below when you are
working with text range object.
' I know of at least one PowerPoint bug where it will error out - when
an image has been dragged/pasted
' into a text box. In such a case, both HasTextFrame and HasText
properties will return TRUE but PowerPoint
' will throw an error when you try to retrieve the text.
' On Error Resume Next
Select Case oShp.Type
Case 19 'msoTable
For iRows = 1 To oShp.Table.Rows.Count
For iCol = 1 To oShp.Table.Rows(iRows).Cells.Count
Set oShpTmp = oShp.Table.Rows(iRows).Cells(iCol).Shape:
oShpTmp.Select
Call ReplaceText(oShpTmp, FindString, ReplaceString)
Next
Next
Case msoGroup 'Groups may contain shapes with text, so look within it
For I = 1 To oShp.GroupItems.Count
Call ReplaceText(oShp.GroupItems(I), FindString, ReplaceString)
Next I
Case 21 ' msoDiagram
For I = 1 To oShp.Diagram.Nodes.Count
Call ReplaceText(oShp.Diagram.Nodes(I).TextShape, FindString,
ReplaceString)
Next I
Case Else
If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
Set oTxtRng = oShp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString,
Replacewhat:=ReplaceString, WholeWords:=True)
Do While Not oTmpRng Is Nothing ' If you are using PPT 2007
change this line to Do While oTmpRng.Text<>"" .
' Do While oTmpRng.Text <> ""
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString,
Replacewhat:=ReplaceString, _
After:=oTmpRng.Start +
oTmpRng.Length, WholeWords:=True)
Loop
End If
End If
End Select
End Sub
 
J

John Wilson

I get that error if I don't change the line as indicated for 2007?

I get an object variable not set for oTmpRange.Text when I do but I havn't
time to investigate fully right now
Also noticed that you Dim Icols but use Icol in the code which will only
work if you don't have Option Explicit set as default
 
J

John Svendsen

Hi JW, thanks for replying:
I was not clear about explaining where I'm encountering my problem- let me
try better:
I've modified the code (new code below) to make the problem I'm running into
more clear
When I open a new presentation (only in Powerpoint 2007), add a table to it,
and run the code below step-by-step (F8) I see it go to XYZ_recurse once,
skip through the code until "Call XYZ_recurse(oShpTmp)" for Case 10
(table), sub XYZ_recurse is called a second time and when I come to code "
Select Case oShp.Type" the second time if fails!!! Do you also see this
failure?

Again, thanks so much for you attention and time
Rgds, JS

'=================================================================
Sub XYZ()
Dim oPres As Presentation, oSld As Slide, oShp As Shape
For Each oSld In ActivePresentation.Slides 'For Each oSld In oPres.Slides
For Each oShp In oSld.Shapes
Call XYZ_recurse(oShp)
Next oShp
Next oSld
End Sub
Sub XYZ_recurse(oShp As Object)
Dim oTxtRng As TextRange, oTmpRng As TextRange, oShpTmp As Shape
Dim I As Integer, iRows As Integer, iCols As Integer
'-----------------------------------------
Select Case oShp.Type '<<< ERROR HERE!!!!
'-----------------------------------------
Case 19 'msoTable
For iRows = 1 To oShp.Table.Rows.Count
For iCols = 1 To oShp.Table.Rows(iRows).Cells.Count
Set oShpTmp = oShp.Table.Rows(iRows).Cells(iCols).Shape
Call XYZ_recurse(oShpTmp)
Next
Next
Case msoGroup 'Groups may contain shapes with text, so look within it
For I = 1 To oShp.GroupItems.Count
Call XYZ_recurse(oShp.GroupItems(I))
Next I
Case 21 ' msoDiagram
For I = 1 To oShp.Diagram.Nodes.Count
Call XYZ_recurse(oShp.Diagram.Nodes(I))
Next I
Case Else
If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
MsgBox "oShp has a HasTextFrame and HasText"
End If
End If
End Select
End Sub
 
S

Shyam Pillai

John,
This fails because the nature of the table shape in PPT 2007 has changed
since 2003 and in the process the object model is no longer compatible with
the new tables. You can no longer reliably use the Shape object associated
with the cell in PPT 2007. So don't use it for table shapes in PPT 2007. I
would strongly recommend that you don't use the Replace method associated
with the TextRange object for table shapes, use the VBA/VB Replace function
instead to work directly on the string.


--
Regards,
Shyam Pillai

Animation Carbon: Copy/Paste/Share animation libraries.
www.animationcarbon.com
 
J

John Svendsen

Hi Shyam,

Thanks so much for replying and your "heads up" about the changes, you are
truly helpful!
Quick questions: [1] Do the changes from 2003 to 2007 also apply to other
shape objects that create trouble when using the Type property for shapes
(e.g., Groups, Diagrams, etc., - or are Tables the only problem?). [2] is
there any way of programmatically calling the user-interface find&replace
function (what we do manually - thus bypassing all these problems)?
Again, thank you for your kind attention and help :)
Rgds, JS
 
S

Shyam Pillai

[1] Do the changes from 2003 to 2007 also apply to other
shape objects that create trouble when using the Type property for shapes
(e.g., Groups, Diagrams, etc., - or are Tables the only problem?).

Groups are not a problem. SmartArts are a problem when you are trying to
manipulate text in them, if you using the object model. You can can treat a
diagram as a group to access the shapes and text in them. BUT if the shape
has more than 1 paragraph of text in it, it will throw an error when you
access it.

[2] is there any way of programmatically calling the user-interface
find&replace function (what we do manually - thus bypassing all these
problems)?
There are workarounds to achieving the desired results using VBA. I'll put
up an updated page which lists them shortly.


--
Regards,
Shyam Pillai

Animation Carbon: Copy/Paste/Share animation libraries.
www.animationcarbon.com



John Svendsen said:
Hi Shyam,

Thanks so much for replying and your "heads up" about the changes, you are
truly helpful!
Quick questions: [1] Do the changes from 2003 to 2007 also apply to other
shape objects that create trouble when using the Type property for shapes
(e.g., Groups, Diagrams, etc., - or are Tables the only problem?). [2] is
there any way of programmatically calling the user-interface find&replace
function (what we do manually - thus bypassing all these problems)?
Again, thank you for your kind attention and help :)
Rgds, JS

Shyam Pillai said:
John,
This fails because the nature of the table shape in PPT 2007 has changed
since 2003 and in the process the object model is no longer compatible
with the new tables. You can no longer reliably use the Shape object
associated with the cell in PPT 2007. So don't use it for table shapes in
PPT 2007. I would strongly recommend that you don't use the Replace
method associated with the TextRange object for table shapes, use the
VBA/VB Replace function instead to work directly on the string.


--
Regards,
Shyam Pillai

Animation Carbon: Copy/Paste/Share animation libraries.
www.animationcarbon.com
 

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