D
David Thielen
Hi;
There is a bug in the Find.Execute code. Microsoft has (finally) acknowleged
the bug but has provided no work around. Below is a method that I believe
provides a work-around for the bug.
It has passed all the tests I have thrown at it. However, as I don't know
the cause of the bug, I can't promist that this code will work 100% of the
time.
Any suggestions, please reply.
--
thanks - dave
/// <summary>
/// Find the passed in text in the document.
/// </summary>
/// <param name="startOffset">Start the find at this offset
(inclusive).</param>
/// <param name="endOffset">End the find at this offset (inclusive).</param>
/// <param name="forward">true if search forward.</param>
/// <param name="text">the text to find.</param>
/// <returns>The range the text is at, null if not found.</returns>
public Range Find (int startOffset, int endOffset, bool forward, string
text)
{
// Word sometimes won't find it if it starts at startOffset. So we start
1 earlier and if it's
// there also, we error out on the find and do it the hard way below.
int rangeStart = Math.Max(0, startOffset - 1);
// Word API has the end as exclusive, but we treat it as inclusive
int rangeEnd = Math.Min(endOffset + 1, ThisDocument.Content.End);
object start = rangeStart;
object end = rangeEnd;
Range range = ThisDocument.Range (ref start, ref end);
range.Find.ClearFormatting();
range.Find.Forward = forward;
range.Find.Text = text;
range.Find.Wrap = WdFindWrap.wdFindStop;
object missingValue = Type.Missing;
range.Find.Execute(ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
// if in our range, we have it
if (range.Find.Found && (startOffset <= range.Start) && (range.End <=
rangeEnd))
return range;
// there is a bug where it sometimes doesn't find text. So here we do it
the old fashioned way.
// find the start - there may be several tags before the tag we are on in
this para.
// and we might be going forward - or backwards!
start = startOffset;
end = rangeEnd;
range = ThisDocument.Range (ref start, ref end);
string docText = range.Text;
if (docText == null)
return null;
int ind = forward ? docText.IndexOf(text) : docText.LastIndexOf(text);
if (ind == -1)
return null;
// ok, figure out the range for this - can't jump to start + ind as that
can go past the begining (no idea why)
int _start = forward ? startOffset : rangeEnd - text.Length;
while ((startOffset <= _start) && (_start < rangeEnd))
{
start = _start;
end = rangeEnd;
range = ThisDocument.Range (ref start, ref end);
docText = range.Text;
// see if we are now starting on it - make sure it's that tag in our range
if ((docText != null) && docText.StartsWith(text))
{
// get all of it (may have formatting in it)
int endPos = range.Start + text.Length;
while (endPos < endOffset)
{
range.End = endPos;
docText = range.Text;
if (docText == text)
break;
endPos ++;
}
return range;
}
_start += forward ? 1 : -1;
}
return null;
}
There is a bug in the Find.Execute code. Microsoft has (finally) acknowleged
the bug but has provided no work around. Below is a method that I believe
provides a work-around for the bug.
It has passed all the tests I have thrown at it. However, as I don't know
the cause of the bug, I can't promist that this code will work 100% of the
time.
Any suggestions, please reply.
--
thanks - dave
/// <summary>
/// Find the passed in text in the document.
/// </summary>
/// <param name="startOffset">Start the find at this offset
(inclusive).</param>
/// <param name="endOffset">End the find at this offset (inclusive).</param>
/// <param name="forward">true if search forward.</param>
/// <param name="text">the text to find.</param>
/// <returns>The range the text is at, null if not found.</returns>
public Range Find (int startOffset, int endOffset, bool forward, string
text)
{
// Word sometimes won't find it if it starts at startOffset. So we start
1 earlier and if it's
// there also, we error out on the find and do it the hard way below.
int rangeStart = Math.Max(0, startOffset - 1);
// Word API has the end as exclusive, but we treat it as inclusive
int rangeEnd = Math.Min(endOffset + 1, ThisDocument.Content.End);
object start = rangeStart;
object end = rangeEnd;
Range range = ThisDocument.Range (ref start, ref end);
range.Find.ClearFormatting();
range.Find.Forward = forward;
range.Find.Text = text;
range.Find.Wrap = WdFindWrap.wdFindStop;
object missingValue = Type.Missing;
range.Find.Execute(ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
// if in our range, we have it
if (range.Find.Found && (startOffset <= range.Start) && (range.End <=
rangeEnd))
return range;
// there is a bug where it sometimes doesn't find text. So here we do it
the old fashioned way.
// find the start - there may be several tags before the tag we are on in
this para.
// and we might be going forward - or backwards!
start = startOffset;
end = rangeEnd;
range = ThisDocument.Range (ref start, ref end);
string docText = range.Text;
if (docText == null)
return null;
int ind = forward ? docText.IndexOf(text) : docText.LastIndexOf(text);
if (ind == -1)
return null;
// ok, figure out the range for this - can't jump to start + ind as that
can go past the begining (no idea why)
int _start = forward ? startOffset : rangeEnd - text.Length;
while ((startOffset <= _start) && (_start < rangeEnd))
{
start = _start;
end = rangeEnd;
range = ThisDocument.Range (ref start, ref end);
docText = range.Text;
// see if we are now starting on it - make sure it's that tag in our range
if ((docText != null) && docText.StartsWith(text))
{
// get all of it (may have formatting in it)
int endPos = range.Start + text.Length;
while (endPos < endOffset)
{
range.End = endPos;
docText = range.Text;
if (docText == text)
break;
endPos ++;
}
return range;
}
_start += forward ? 1 : -1;
}
return null;
}