J
JuanPablo Jofre
Hi All,
I have a list of selected styles from which I have to work on the text of
all ocurrencies of each style.
Sometimes the Found property of the Find object, after an Execute method
invocation, shows a true value over null text and does not advance to other
regions, with the current style definition, on subsequent Execute invocations
leaving the code on an endless loop.
Can anyone help me with some code or links to code that solves this problem?
I'm including my code for all of you who would like to correct it.
Thanks in advance for your help.
#region Finding Reference statistics
//position at begining of document
extend = Word.WdMovementType.wdMove;
appWord.Selection.HomeKey(ref unitStory, ref extend);
searchRange = appWord.Selection.Range;
//Search for all selected Styles list
foreach (Word.Style FindingStyle in selectedStyles)
{
Object findingStyle = (Object)FindingStyle;
currentFind = searchRange.Find;
currentFind.ClearFormatting();
currentFind.Forward = true;
currentFind.set_Style(ref findingStyle);
FindText = (Object)"";
Wrap = Word.WdFindWrap.wdFindStop;
Format = (Object)true;
// all other variables in Execute method were defined as: Object
xyz = System.Type.Missing;
currentFind.Execute(ref FindText, ref MatchCase, ref
MatchWholeWord, ref MatchWildcards, ref MatchSoundsLike,
ref MatchAllWordForms, ref Forward, ref
Wrap, ref Format, ref ReplaceWith, ref Replace,
ref MatchKashida, ref MatchDiacritics, ref
MatchAlefHamza, ref MatchControl);
while (currentFind.Found)
{
if ((searchRange.Text != null) && ((searchRange.Text !=
"\r") || (searchRange.Text != "\r\a") || (searchRange.Text != "\a\r") ||
(searchRange.Text != "\a")))
{
//Doing here what is needed with the content of the
found style
}
//This section is to be sure that now the starting point for
the Execute method is past the last found style
iPreviousStart = searchRange.Start;
searchRange.Collapse(ref wdCollapseEnd);
searchRange.MoveStart(ref wdWord, ref countUnit);
if (iPreviousStart == searchRange.Start)
{
break;
}
//find the next text within the current searched style
currentFind.Execute(ref FindText, ref MatchCase, ref
MatchWholeWord, ref MatchWildcards, ref MatchSoundsLike,
ref MatchAllWordForms, ref Forward, ref
Wrap, ref Format, ref ReplaceWith, ref Replace,
ref MatchKashida, ref MatchDiacritics,
ref MatchAlefHamza, ref MatchControl);
//safety break in case enters on a loop.... but you might
miss other content with current style
if (searchRange.Text == null)
{
break;
}
}
}
#endregion
I have a list of selected styles from which I have to work on the text of
all ocurrencies of each style.
Sometimes the Found property of the Find object, after an Execute method
invocation, shows a true value over null text and does not advance to other
regions, with the current style definition, on subsequent Execute invocations
leaving the code on an endless loop.
Can anyone help me with some code or links to code that solves this problem?
I'm including my code for all of you who would like to correct it.
Thanks in advance for your help.
#region Finding Reference statistics
//position at begining of document
extend = Word.WdMovementType.wdMove;
appWord.Selection.HomeKey(ref unitStory, ref extend);
searchRange = appWord.Selection.Range;
//Search for all selected Styles list
foreach (Word.Style FindingStyle in selectedStyles)
{
Object findingStyle = (Object)FindingStyle;
currentFind = searchRange.Find;
currentFind.ClearFormatting();
currentFind.Forward = true;
currentFind.set_Style(ref findingStyle);
FindText = (Object)"";
Wrap = Word.WdFindWrap.wdFindStop;
Format = (Object)true;
// all other variables in Execute method were defined as: Object
xyz = System.Type.Missing;
currentFind.Execute(ref FindText, ref MatchCase, ref
MatchWholeWord, ref MatchWildcards, ref MatchSoundsLike,
ref MatchAllWordForms, ref Forward, ref
Wrap, ref Format, ref ReplaceWith, ref Replace,
ref MatchKashida, ref MatchDiacritics, ref
MatchAlefHamza, ref MatchControl);
while (currentFind.Found)
{
if ((searchRange.Text != null) && ((searchRange.Text !=
"\r") || (searchRange.Text != "\r\a") || (searchRange.Text != "\a\r") ||
(searchRange.Text != "\a")))
{
//Doing here what is needed with the content of the
found style
}
//This section is to be sure that now the starting point for
the Execute method is past the last found style
iPreviousStart = searchRange.Start;
searchRange.Collapse(ref wdCollapseEnd);
searchRange.MoveStart(ref wdWord, ref countUnit);
if (iPreviousStart == searchRange.Start)
{
break;
}
//find the next text within the current searched style
currentFind.Execute(ref FindText, ref MatchCase, ref
MatchWholeWord, ref MatchWildcards, ref MatchSoundsLike,
ref MatchAllWordForms, ref Forward, ref
Wrap, ref Format, ref ReplaceWith, ref Replace,
ref MatchKashida, ref MatchDiacritics,
ref MatchAlefHamza, ref MatchControl);
//safety break in case enters on a loop.... but you might
miss other content with current style
if (searchRange.Text == null)
{
break;
}
}
}
#endregion