Reply
 
Thread Tools Display Modes
  #1   Report Post  
Robix
 
Posts: n/a
Default Why the returned value of the InStr function differs from the real one?

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



  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default

On Wed, 6 Apr 2005 14:56:06 +0200, "Robix"
wrote:

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
  #3   Report Post  
Robix
 
Posts: n/a
Default

Hi Jay,

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

{0Original string}number{Translated string0}{0Original
string}number{Translated string0}...

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


"Jay Freedman" wrote in message
...
On Wed, 6 Apr 2005 14:56:06 +0200, "Robix"
wrote:

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



  #4   Report Post  
Graham Mayor
 
Posts: n/a
Default

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




Robix wrote:
Hi Jay,

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

{0Original string}number{Translated string0}{0Original
string}number{Translated string0}...

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


"Jay Freedman" wrote in message
...
On Wed, 6 Apr 2005 14:56:06 +0200, "Robix"
wrote:

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



  #5   Report Post  
Robix
 
Posts: n/a
Default

Hi Graham,

Thank you for your help. I'll try your code.

Regards,
Robert

"Graham Mayor" wrote in message
...
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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




Robix wrote:
Hi Jay,

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

{0Original string}number{Translated string0}{0Original
string}number{Translated string0}...

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


"Jay Freedman" wrote in message
...
On Wed, 6 Apr 2005 14:56:06 +0200, "Robix"
wrote:

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org







  #6   Report Post  
Robix
 
Posts: n/a
Default

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

"Graham Mayor" wrote in message
...
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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




Robix wrote:
Hi Jay,

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

{0Original string}number{Translated string0}{0Original
string}number{Translated string0}...

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


"Jay Freedman" wrote in message
...
On Wed, 6 Apr 2005 14:56:06 +0200, "Robix"
wrote:

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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org





Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Compare and Merge Document Function EricM Microsoft Word Help 2 March 28th 05 11:53 PM
Your "Help" function has become totally useless Madison Sprague Microsoft Word Help 1 March 19th 05 01:27 AM
Stop Overtype Function from popping up while using Word Cannery Works Microsoft Word Help 3 February 8th 05 04:09 PM
Can't Open Data Source (Query) where criteria is defined by functi l_stocky Mailmerge 5 January 21st 05 03:01 PM
HitTest function does not return node as documented Balajee S Microsoft Word Help 5 November 25th 04 12:04 AM


All times are GMT +1. The time now is 08:25 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"