Home |
Search |
Today's Posts |
#1
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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 | |
|
|
![]() |
||||
Thread | Forum | |||
Word Compare and Merge Document Function | Microsoft Word Help | |||
Your "Help" function has become totally useless | Microsoft Word Help | |||
Stop Overtype Function from popping up while using Word | Microsoft Word Help | |||
Can't Open Data Source (Query) where criteria is defined by functi | Mailmerge | |||
HitTest function does not return node as documented | Microsoft Word Help |