Publisher PIA change of behavior for TextRange.Text between Office 2003 and 2007

B

Bob Eaton

I have a .Net-based COM add-in that works in Publisher in which I go thru
the "Stories" one-by-one and change the encoding of words (also, one-by-one)
in-situ.

I iterate the Paragraphs in a Story and for each Paragraph, I use a
TextRange object to isolate a single word (and it's following space) to do
the replacement. So, for example, if I had a Paragraph that contained:

// aTextRange.Text = "This is a paragraph\r";

Then I would make a working copy of that TextRange and set the StartIndex =
0 (the beginning of the range) and the EndIndex = 5 (the offset of the 'i'
in 'is') and this would result in:

myTextRange.Text = "This "

Then I select that range (i.e. myTextRange.Select()) and then set the
".Text" property to my new encoding of that word (e.g. for Piglatin,
"myTextRange.Text = "Isthay ").

Now the problem: in Office 2003, even though my replacement text is longer,
it only replaces the selected portion of the original range (resulting in
the original range becoming "Isthay is a paragraph\r").

But in Office 2007, it seems to ignore what portion of the TextRange is
selected and replaces the length of the new string (resulting in the
original range becoming "Isthay paragraph"--I.e. clobbering the next
word(s), because the replacement string is longer than the original word).

Is there some way to work around this? That is, I know that I can use the
_Application.Version to detect whether I'm talking to Publisher 2003 or 2007
and treat them differently, but I'm not sure how to work with the TextRange
object in 2007 to make it behave like the 2003 PIA does.

Any ideas?

Thanks,
Bob

P.S. This is only a problem with the Publisher PIAs. I do the same thing in
Excel, Access, and Word (of course, with the RangeText object, instead), and
it works the same in both the 2003 and 2007 versions.
 
M

Miguel Gonzalez

This is probably a bug in Publisher since it should automatically expand your
textrange in that case to accomodate your new value without changing the rest
of the text in the textbox.

What you are trying to do can also be achieved by using the Find class from
the TextRange:

myTextRange.Find = myTextRange.Characters(StartIndex, EndIndex).Text;
myTextRange.ReplaceScope =
Microsoft.Office.Interop.Publisher.PbReplaceScope.pbReplaceScopeOne;
myTextRange.Find.ReplaceWithText = "Isthay"
myTextRange.Find.Execute

I tried this in 2007 and it seems to work fine. I think this code will work
both in 2003 and 2007 so you might not need to check the version if you use
Find for this case.

Hope this helps,
Miguel
 
B

Bob Eaton

Thanks Miguel,

The "Find" class looks promising, but I'm having two problems with it.

In testing another kind of replacement algorithm (in which I reverse the
input string), my "FindText" was "This" (for the first word), and the
"ReplaceWithText" was "sihT".

My first problem is that after doing the Execute, the paragraph.Text became
"SihT is a paragraph". That is, it seems that Publisher is doing automatic
spelling corrections--in this case, capitalizing the first word of the
sentence--which I don't want. So my first question is, is there a way to
prevent that from happening.

The second problem is that suppose that my original paragraph were "This is
SihT paragraph" (I.e. the third word is the same as the result of
transforming the first word). Once the first two words are replaced, the
paragraph becomes "SihT si SihT paragraph". In this situation, when I do the
next Execute (with FindText="SihT"), it replaces the *first* occurrence of
"SihT" rather than the 2nd one that I want. That is, it is replacing an
already processed value which in my application is a no-no.

I'm using the same Find object for each replacement, so it's not like I'm
querying for a new Find object which might start over... Is there some way
to force this to remember where it left off and restart the search from
there?

Thanks again,
Bob
 

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