Why the returned value of the InStr function differs from the real one?

R

Robix

Hi,

I am writing macro that changes the color of the part of the text. For
determining the start and end position I use the InStr function. Text is in
the table. The returned value of the InStr function is correct only for the
cell(1,1). As soon as it moves to the next cell, the returned value
(character position) is different from the real one (it is one more than it
should be in the cell(1,2)). This difference increases as it moves from one
cell to another.

Can someone help me with this?

Thank you very much in advance.
Regards,
Robert
 
J

Jay Freedman

Hi,

I am writing macro that changes the color of the part of the text. For
determining the start and end position I use the InStr function. Text is in
the table. The returned value of the InStr function is correct only for the
cell(1,1). As soon as it moves to the next cell, the returned value
(character position) is different from the real one (it is one more than it
should be in the cell(1,2)). This difference increases as it moves from one
cell to another.

Can someone help me with this?

Thank you very much in advance.
Regards,
Robert

Hi Robert,

The InStr function counts all the characters in a string, including
the nonprinting ones. For instance, each end-of-cell mark (which
appears as ¤ when you display nonprinting characters) occupies two
characters in the document.

In any case, stepping though the cells one at a time is extremely
inefficient. You're usually better off with a Replace operation, which
can be used just to change formatting if you choose. Tell us more
about how you decide which text to recolor, and we can suggest a
better way to program it.
 
R

Robix

Hi Jay,

I have files containing original strings followed by the translated ones in
this way:

{0>Original string<}number{>Translated string<0}{0>Original
string<}number{>Translated string<0}...

I have to color original strings with one color and translated ones with
another one, depending on the number. I am using InStr function to search
for "{0>" to find start of the original string, "<}" to search for the end
of it, etc. The macro works fine on strings that are not in the table
(though I must admit it is slow). But as soon as it enters the table the
highlight shifts to the right (except the cell(1,1)), so in the cell(1,2)
the first char of the string is not highlighted (but should be) and < at the
end is highlighted (but should not be).

Thank you for your help in advance.
Robert
 
G

Graham Mayor

Like Jay I would have thought the replace function far more efficient. The
following sets the colours to red and blue - it should be pretty obvious
where you need to change the code for your own colour choice.

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\{0\>*\<\}"
.Replacement.Text = "^&"
.Replacement.Font.Color = wdColorRed
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\{\>*\<0\}"
.Replacement.Text = "^&"
.Replacement.Font.Color = wdColorBlue
End With
Selection.Find.Execute replace:=wdReplaceAll

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Robix

Hi both Jay and Graham,

It works perfect and it is much much faster than my macro that uses the
InStr function.

Thank you both very much
Regards,
Robert
 

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