How to insert a string before the text of a selected word

  • Thread starter Thread starter david.f.jenkins
  • Start date Start date
D

david.f.jenkins

I have an application where I'm looping through and editing words in a
text range. As part of that editing, it may occur that I find that I
need to insert a string before the token being examined. I'm having
difficulty doing that, because after I make the insertion, my ranges
appear to be ill-defined.

For instance, I've tried using

tRange.word(i).InsertBefore "abc " where the text of tRange.word(i) is
"defghi" but after the insertion, tRange.words(i).text = "abc de". The
problem is that later on downstream I want examine the "defghi" token
for some further tests, but it no longer exists as
tRange.words(i).text, since that is returned as "abc de"

This is all done in a sub, where I've passed in the index to the word,
and the textrange that contains all the originally selected text. Even
though the new text has now been included in the selection (you can see
that happen in debug), the tRange/index parameters to the sub stil
combine to point to only the first 6 letters of the words(i) token.
From then on, I'm doomed!

I need some way to update the object pointed to by tRange to include
the new expanded selection - is there a way to do that from within the
sub?
 
Messing with TextRanges can be tricky. You might be well-served to go
backwards through the Text, rather than Forward. That is, if you have 100
characters in teh TextRange, inserting something before character 5, will
shift the other 95 characters. So instead start backwards, and do what
you want to do to the characters at the end first. Inserting something
before characther 90 will only shift the last 11 characters so if the
next thing you want to do is play with character 89, you should still be
OK.

At least, this is how I remember it; it's been a while since I've played
with TextRanges extensively.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @b28g2000cwb.googlegroups.com:
 
Thanks, David, for the suggestion.

I'm not sure that will do what I need, though. I'm really processing
in terms of tokens (aka "Words"), and so once I've inserted some text
at the start of one token, that token appers to be dorked up. For
example, if I'm working on token "network" and insert "fast " in front
of the text for that token, PPT treats the original word from then on
as "fast rk". Since I have a need downstream to process both "fast"
and "network" as separate tokens, I'm kind screwed.

What I am looking for is a way to get VBA (I guess) to recompute his
original text range of "network" into a new text range "fast network".
Since I'm dealing with selected text here, I may be able to simply
reset the textrange as the selection after the inertbefore - I havn't
tried that approach yet.
 
I think I've doped out a solution (however inelegant it might be):
Each time I insert my text before, I reinitialize the range collection
holding all the selected ranges on the slide. I then recursively call
the insertion routine with the revamped collection. since the
requisite stuff has been inserted, it doesn't get in an infinte loop,
and ultimately pops out the sub with all inserted correctly.

I should say, I guess, "...at least so far, it appears to be working
correctly." I've been in this business too long to never say never...
 
I have an application where I'm looping through and editing words in a
text range. As part of that editing, it may occur that I find that I
need to insert a string before the token being examined. I'm having
difficulty doing that, because after I make the insertion, my ranges
appear to be ill-defined.

One approach is to store the start/length of each range (eg to an array) before
you start messing with the text. Then work through the array rather than the
ranges collection. As you add/subtract text, you can then offset the
subsequent start/length values accordingly.
 
Back
Top