Troubles iterating over found styles because enters in a loop

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
 
K

Klaus Linke

Hi JuanPablo,

You'll probably have to collapse the currentFind Range (to its end) at the
end of the while (currentFind.Found) loop.
I didn't test your code, but it seems a bit strange that you use
SearchRange.Text where I would have expected currentFind.Text:
if ((searchRange.Text != null) ...
After currentFind.Execute, currentFind is the Range of your match, not
SearchRange?

Klaus
 
K

Klaus Linke

Sorry, I had (still have) some problems "translating" your code to VBA in my
mind (partly because of missing declarations), and didn't see the
".Collapse" in your code.
Still, the problem might be that currentFind.Execute messes with searchRange
(= sets it to the match), which you don't seem to expect.

If you take that into account, you can probably simplify your code quite a
bit. You just have to (re)set searchRange to the range from the end of your
match to the end of whatever you want to search after each match, and can
get rid of iPreviousStart and the comparisons you make.

The code followingalso seems redundant... you already loop all matches, don't you?

Likely also redundant:(what's countUnit? You likely don't need to move after you've collapsed)

If you work with styles, another problem you often encounter is with the
different style types (character, list. table styles) when you likely only
want to deal with paragraph styles.
Since I don't know how you set selectedStyles, I can't tell whether that may
be a problem.

In VBA, I'd use "With":
With currentFind
.ClearFormatting
' ...
.Execute
While .Found
'...
Wend
End With

That might make the code a bit easier to read. Not sure how that's done in
C#?

Klaus





Klaus Linke said:
Hi JuanPablo,

You'll probably have to collapse the currentFind Range (to its end) at the
end of the while (currentFind.Found) loop.
I didn't test your code, but it seems a bit strange that you use
SearchRange.Text where I would have expected currentFind.Text:
if ((searchRange.Text != null) ...
After currentFind.Execute, currentFind is the Range of your match, not
SearchRange?

Klaus
 

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