How to insert text in a textrange? It's been a year now, and I'm *still* stuck!

D

david.f.jenkins

I have written a SUB that receives a textrange as an argument. In the
SUB, I replace some of the text in the textrange. Before I make the
replacement in the SUB, I extend the length of the textrange an
appropriate number of characters by using the InsertAfter method.
After I do the InsertAfter, the Length property appears to have been
adjusted.

I then do a Replace. If the replacing text is longer than the replaced
text, the TextRange appears to get arbitrarily reset to all of the text
in the enclosing shape. (The textrange in question is selected text in
that shape.) And yet a debug view of the argument in the call to the
SUB shows a textrange with the original length at the time of the call,
but with the changed text. As far as the calling routine is concerned,
the text has been changed, but the length has not.

How can that be?

I'm calling by Ref, BTW. If I change the textrange with the Replace
and InsertAfter methods, shouldn't it be changed when I return to the
calling routine? Am I misundertanding how the object references are
being resolved?

I just want to insert text in a textrange, and have the length of the
range get adjusted accordingly. It's been a year or so that I've been
toying with this problem off and on -- if it's impossible to do, just
tell me, so that I can get on with my life!
 
B

Bill Dilworth

The best idea would be to post the relevant code. I am not sure what it is
you are asking and fixing code is easier than writing a bunch of code that
may not fit your solution.


--
Bill Dilworth
A proud member of the Microsoft PPT MVP Team
Users helping fellow users.
http://billdilworth.mvps.org
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
vestprog2@ Please read the PowerPoint FAQ pages.
yahoo. They answer most of our questions.
com www.pptfaq.com
..
 
D

david.f.jenkins

Hi Bill:

I'm not ignoring your good advice - I'm just struggling with distilling
all the actual application code to come up with a simplified snippet
that will demonstrate the problem I'm having. As soon as I get that
done I'll post it here.
 
D

david.f.jenkins

Ok: first of all, I'm thinking my problem has more to do with passing
of arguments and the creation of textranges by textrange methods, but
let's see. Here is some code:

Option Explicit
Option Base 0
Option Compare Text

Sub TextRangeTest()

Dim i, j, k As Integer
Dim pattern As String
Dim coll As New Collection

coll.Add ActiveWindow.Selection.TextRange

With ActiveWindow.Selection

For i = 1 To 3

Select Case i
Case 1
pattern = "x"
Case 2
pattern = "y"
Case 3
pattern = "zz"
End Select

' This code deosn't work
'For j = 1 To coll(1).Paragraphs.Count
' For k = 1 To coll(1).Paragraphs(j).Sentences.Count
' textInsert coll(1).Paragraphs(j).Sentences(k), pattern
' Next k ' sentence
'Next j ' paragraph

' this code does
For j = 1 To ActiveWindow.Selection.TextRange.Paragraphs.Count
For k = 1 To
ActiveWindow.Selection.TextRange.Paragraphs(j).Sentences.Count
textInsert
ActiveWindow.Selection.TextRange.Paragraphs(j).Sentences(k), pattern
Next k ' sentence
Next j ' paragraph

Next i

End With

End Sub

Sub textInsert(t As TextRange, find As String)

Dim l As Integer
Dim foundRange As TextRange


l = Len(t.Text)
t.InsertAfter Space(2) ' this doesn't do
what I want, when using the coll
t.Replace find, Space(2) & find
t.Characters(l + 2 + 1, 2).Delete ' this doesn't do what I
want when using the coll

End Sub

If you will create a slide with a text box containing "xyzz", select
the text and run TextRangeTest, you can see what I'm talking about. In
the case where I'm processing the ActiveWindow.Selection textrange
directly, all 3 texts ("x", "y" and "zz") end up having 2 spaces
inerted in front of them, and the selection is expanded accordingly.
When I add the selection textrange to a collection, however (which is
what I'm doing in my real program), the code in the TextInsert is not
really working on the selection object, and hence the program fails to
do what I want.

The crux of my problem is this: I need to create a collection of
textranges from the slide (they may or may not be selected - I don't
think that matters, though), pass them to a Sub where the Sub will make
substitutions of subsets of the textrange texts, based on one criteria
or another. I thought building the collection would be a good way to
start, rather than going through these one by one as I dscover them on
the slide, but now I'm beginning to change my mind.

Can you point out how to make the above code work, and still maintain
the use of the collection?

Thanks for your help. (BTW, coming up with this shortened version of
the problem has been highly educational. Not sure I'd recommend it to
a friend I wanted to keep, but I've certainly learned a lot. Haven't
got much sleep, though...)
 
D

david.f.jenkins

Thanks, Steve.

I've about worreid this thing to death. As usual, I think I've
overcomplicated my problem, and I've written some farily code to adjust
when replacing text that's shorter than the replacement value. As you
say, once the first replacement's been made, I just have to adjust
where I start looking for a match to make the next replacement. So
far, it's been working ok.

Thanks for taking the time to think about it - you're always a big
help, when it's 3 in the AM and nobody loves you and deadlines are
looming...
 
D

david.f.jenkins

Here's a couple of things that I was dealing with that caused me grief.
I pass these along in case someone else is having similar
difficulties:

This first one turned out to be a blessing in disguise. Suppose you're
dealing with a text range that's just a portion of the text in some
placeholder - a sentence from some paragraph in a textbox, say. If you
do a replace in that sentence that essentially lengthens the sentence,
then you'll see (if you're in the debugger, say, where I spent a *lot*
of my time :-( ) that the textrange text now contains all of the text
from the parent placeholder. (I think that's what it is, but since the
debugger won't show more than anbout 255 bytes of text, I'm not real
sure about that.) Nevertheless, if you're working your way across the
text, making identical replaces as you go, it's imprtant that you be
able to see text that would have otherwise been "shoved off the right"
by the first replace, and this you can do by looping, and using the
"new" textrange at each iteration. I had originally recursed to do the
repetitive replaces, and that just ended making me balder than I
already was.

The second thing that *really* messed me up is this: if you've got a
text range ("trange," let's call it) that includes a bunch of bullets
and multi-line texts, you may find that len(trange.text) and
trange.Length are two different values! I suspect that the latter
contains a count for all the CR/LFs in the string, but have not looked
much further than that. However, when computing character positions
for trange.Characters(...) you better know which counts you're dealing
with.

Well, anyway, I've pretty much got my regex search/replace PowerPoint
functionality working to my satisfaction. Any idea if the Office 2007
stuff will support regex seraching/replacing?
 

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