PP2007 Bug: Selection.TextRange.Find Uses "parent" TextRange

G

Guest

Say you've got three bullets with identical text in a shape on a slide. You
select the middle one, and use
activewindow.selection.textrange.find
to find, then replace text. If the code in the help file is used, text will
also be replaced in the first and last bulleted paragraphs.

Here is a work-around. To illustrate, place HCl -> H2O in a slide, where it
says "Click to add text". Place it in there at least three times. Select
the middle paragraph (the middle bulleted item, if the bullets are showing).
Here's the code:

Sub play()
' Replace "->" with a prettier arrow
Dim trPartial As TextRange
Dim trFoundText As TextRange
Dim lAfter As Long
Set trPartial = ActiveWindow.Selection.TextRange
Set trFoundText = trPartial.Find(FindWhat:="->", after:=trPartial.Start -
1)
Do While Not (trFoundText = "")
With trFoundText
' Make sure the found text isn't after the end of the selection
If .Start < trPartial.Start - 1 + trPartial.Length Then
.InsertSymbol FontName:="Symbol", charnumber:=174, unicode:=False
lAfter = .Start + .Length
Else
Exit Do
End If
End With
Set trFoundText = trPartial.Find(FindWhat:="->", after:=lAfter)
Loop
End Sub

Running that should replace the -> with a prettier arrow, but only in the
selected paragraph. What's new is that a value for "after" must be
specified, or else Find will start at the beginning of the text in the shape,
rather than at the beginning of the TextRange it was given. Likewise, it
will keep going to the end of the shape's TextRange, rather than the end of
the selection.

A nice feature of Find is that it can find unicode characters. If you type
--> in a shape, PowerPoint will automatically convert it to an (ugly) arrow,
provided autocorrect is turned on. You can select just that arrow, then in
the immediate window type
? ascw(ActiveWindow.Selection.TextRange)
-3872 is displayed.
Include the following in BOTH Find commands:
FindWhat:=ChrW(-3872)
Run the code and it will find and replace the ugly arrows with prettier ones.

Hope this saves somebody some time.

Chris
 
S

Steve Rindsberg

Have a play with this instead:

Sub playnicermaybe()
' Replace "arrow" with a prettier arrow
' change "arrow" to whatever else you want to look for

Dim trPartial As TextRange
Dim trReplaceRange As TextRange
Dim lPosition As Long

Set trPartial = ActiveWindow.Selection.TextRange
lPosition = InStr(trPartial, "arrow")
If lPosition > 0 Then ' found an instance
Set trReplaceRange = trPartial.Characters(lPosition, Len("arrow"))
Call trReplaceRange.InsertSymbol("Symbol", 174, False)

End If


End Sub
 
G

Guest

Christopher King said:
Say you've got three bullets with identical text in a shape on a slide. You
select the middle one, and use
activewindow.selection.textrange.find
to find, then replace text. If the code in the help file is used, text will
also be replaced in the first and last bulleted paragraphs.

Here is a work-around. To illustrate, place HCl -> H2O in a slide, where it
says "Click to add text". Place it in there at least three times. Select
the middle paragraph (the middle bulleted item, if the bullets are showing).
Here's the code:

Sub play()
' Replace "->" with a prettier arrow
Dim trPartial As TextRange
Dim trFoundText As TextRange
Dim lAfter As Long
Set trPartial = ActiveWindow.Selection.TextRange
Set trFoundText = trPartial.Find(FindWhat:="->", after:=trPartial.Start -
1)
Do While Not (trFoundText = "")
With trFoundText
' Make sure the found text isn't after the end of the selection
If .Start < trPartial.Start - 1 + trPartial.Length Then
.InsertSymbol FontName:="Symbol", charnumber:=174, unicode:=False
lAfter = .Start + .Length
Else
Exit Do
End If
End With
Set trFoundText = trPartial.Find(FindWhat:="->", after:=lAfter)
Loop
End Sub

Running that should replace the -> with a prettier arrow, but only in the
selected paragraph. What's new is that a value for "after" must be
specified, or else Find will start at the beginning of the text in the shape,
rather than at the beginning of the TextRange it was given. Likewise, it
will keep going to the end of the shape's TextRange, rather than the end of
the selection.

A nice feature of Find is that it can find unicode characters. If you type
--> in a shape, PowerPoint will automatically convert it to an (ugly) arrow,
provided autocorrect is turned on. You can select just that arrow, then in
the immediate window type
? ascw(ActiveWindow.Selection.TextRange)
-3872 is displayed.
Include the following in BOTH Find commands:
FindWhat:=ChrW(-3872)
Run the code and it will find and replace the ugly arrows with prettier ones.

Hope this saves somebody some time.

Chris

Hi Chris:

I think this is probably the same problem I've been running into with
TextRange.Replace and posted elsewhere today. Unfortunately, I can no longer
test on 2003, and so I can't be absolutely sure that the .Replace behavior
has changed. Even if it hasn't changed, it just doesn't seem that it
oeprates the way it should: if I want to do a replace on the text in a
TextRange, it would seem to me that all I should have to do is select the
text of interest and not be concerned with the rest of the text in the
enclosing shape -- isn't that the way you see it?
 

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