Find method failure in Word COM Addin

M

Mark Wilson

I have a Word COM Addin that needs to find and delete a string of text that
is in the body of an Outlook 2003 e-mail message. The ASCII string is
converted to a BSTR an passed as a parameter to a routine called
FindTextString. The string exists in the document because it was previously
inserted with a call to InsertAfter. As long as the length of the search
string is 256 characters or less, the Find works properly. If the search
string is 257 characters or larger, the call to Find fails with error code
0x80020009 (Exception occurred).

Is there a size restriction on Find?

----------------------------

BOOL CClassWord::FindTextString(BSTR bsTextString)
{
HRESULT hr;
bool bResult;

VARIANT vtDocument; VariantInit(&vtDocument);
VARIANT vtRange; VariantInit(&vtRange);
VARIANT vtFind; VariantInit(&vtFind);
VARIANT vtParam; VariantInit(&vtParam);
VARIANT vtParam1[15]; VariantInit(vtParam1);

hr = GetProperty(m_pParentApp,L"ActiveDocument",&vtDocument);
hr = GetProperty(vtDocument.pdispVal, L"content", &vtRange);
hr = GetProperty(vtRange.pdispVal, L"Find", &vtFind);

vtParam1[14].vt = VT_BSTR; vtParam1[14].bstrVal =
::SysAllocString(bsTextString);
vtParam1[13].vt = VT_ERROR; vtParam1[13].scode = DISP_E_PARAMNOTFOUND;
vtParam1[12].vt = VT_ERROR; vtParam1[12].scode = DISP_E_PARAMNOTFOUND;
vtParam1[11].vt = VT_ERROR; vtParam1[11].scode = DISP_E_PARAMNOTFOUND;
vtParam1[10].vt = VT_ERROR; vtParam1[10].scode = DISP_E_PARAMNOTFOUND;
vtParam1[9].vt = VT_ERROR; vtParam1[9].scode = DISP_E_PARAMNOTFOUND;
vtParam1[8].vt = VT_ERROR; vtParam1[8].scode = DISP_E_PARAMNOTFOUND;
vtParam1[7].vt = VT_ERROR; vtParam1[7].scode = DISP_E_PARAMNOTFOUND;
vtParam1[6].vt = VT_ERROR; vtParam1[6].scode = DISP_E_PARAMNOTFOUND;
vtParam1[5].vt = VT_ERROR; vtParam1[5].scode = DISP_E_PARAMNOTFOUND;
vtParam1[4].vt = VT_ERROR; vtParam1[4].scode = DISP_E_PARAMNOTFOUND;
vtParam1[3].vt = VT_ERROR; vtParam1[3].scode = DISP_E_PARAMNOTFOUND;
vtParam1[2].vt = VT_ERROR; vtParam1[2].scode = DISP_E_PARAMNOTFOUND;
vtParam1[1].vt = VT_ERROR; vtParam1[1].scode = DISP_E_PARAMNOTFOUND;
vtParam1[0].vt = VT_ERROR; vtParam1[0].scode = DISP_E_PARAMNOTFOUND;

hr = CallMethod(vtFind.pdispVal, L"Execute", &vtParam, 15, vtParam1);
::SysFreeString(vtParam1[14].bstrVal);

if (hr == 0x80020009)
MessageBox(NULL, "Exception occurred.", "FindTextString", MB_OK);

if (vtParam.boolVal == VARIANT_TRUE) bResult = true;
else bResult = false;

VariantClear(vtParam1);
VariantClear(&vtParam);
VariantClear(&vtFind);
VariantClear(&vtRange);
VariantClear(&vtDocument);

return bResult;
}
 
P

Peter Huang [MSFT]

Hi Mark,

Thanks for your posting and finding.

This is a limitation of the Find/Replace in Word. It is limited to 255
characters.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Wilson

Thanks for confirming that.

I guess I'll have to break the search string up into smaller pieces and find
each one separately. Then compare the Range start and end values to see if
there in a contiguous block that matches what I'm searching for.
 
P

Peter Huang [MSFT]

Hi Mark,

I just wonder why you need find such a long string.
Also have you tried to use widecard match.
e.g.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "asd*f"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

You can find the information below in Office Help.

<quote>
Search by using wildcards
Use wildcards to find and replace

For example, use the asterisk (*) wildcard to search for a string of
characters ("s*d" finds "sad" and "started").

On the Edit menu, click Find or Replace.
If you don't see the Use wildcards check box, click More.
Select the Use wildcards check box.
Enter a wildcard character in the Find What box. Do one of the following:
To choose a wildcard character from a list, click Special, click a wildcard
character, and then type any additional text in the Find what box.
Type a wildcard character directly in the Find what box.
If you want to replace the item, enter what you want to use as a
replacement in the Replace with box.
Click Find Next, Replace, or Replace All.
To cancel a search in progress, press ESC.

Notes

When the Use wildcards check box is selected, Word finds only the exact
text you specify. (Notice that the Match case and Find whole words only
check boxes are unavailable (dimmed) to indicate that these options are
automatically on; you can¡¯t turn off these options.)
To search for a character that's defined as a wildcard, type a backslash
(\) before the character. For example, type \? to find a question mark.
Wildcards for items you want to find and replace

To find:
Any single character
Type ?
For example, s?t finds "sat" and "set".
Any string of characters
Type *
For example, s*d finds "sad" and "started".
The beginning of a word
Type <
For example, <(inter) finds "interesting" and "intercept", but not
"splintered".

The end of a word
Type >
For example, (in)> finds "in" and "within", but not "interesting".
One of the specified characters
Type [ ]
For example, w[io]n finds "win" and "won".

Any single character in this range
Type [-]
For example, [r-t]ight finds "right" and "sight". Ranges must be in
ascending order.
Any single character except the characters in the range inside the brackets
Type [!x-z]
For example, t[!a-m]ck finds "tock" and "tuck", but not "tack" or "tick".

Exactly n occurrences of the previous character or expression
Type {n}

For example, fe{2}d finds "feed" but not "fed".

At least n occurrences of the previous character or expression
Type {n,}

For example, fe{1,}d finds "fed" and "feed".

From n to m occurrences of the previous character or expression
Type {n,m}

For example, 10{1,3} finds "10", "100", and "1000".

One or more occurrences of the previous character or expression
Type @

For example, lo@t finds "lot" and "loot".

Notes

You can use parentheses to group the wildcard characters and text and to
indicate the order of evaluation. For example, type <(pre)*(ed)> to find
"presorted" and "prevented".
You can use the \n wildcard to search for an expression and then replace it
with the rearranged expression. For example, type (Newton) (Christie) in
the Find what box and \2 \1 in the Replace with box. Word will find "Newton
Christie" and replace it with "Christie Newton".
</quote>



Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Wilson

Thanks Peter.

A wildcard search will not work in this case. I've previously inserted a
specific paragraph of text into the document. There is a possibility that
the paragraph may exist multiple times in the document and and it only needs
to have one copy so I'm searching for an "exact" match on all of the text
taht was previously inserted and then deleting that Range of text.
--
Mark Wilson



"Peter Huang" said:
Hi Mark,

I just wonder why you need find such a long string.
Also have you tried to use widecard match.
e.g.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "asd*f"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

You can find the information below in Office Help.

<quote>
Search by using wildcards
Use wildcards to find and replace

For example, use the asterisk (*) wildcard to search for a string of
characters ("s*d" finds "sad" and "started").

On the Edit menu, click Find or Replace.
If you don't see the Use wildcards check box, click More.
Select the Use wildcards check box.
Enter a wildcard character in the Find What box. Do one of the following:
To choose a wildcard character from a list, click Special, click a wildcard
character, and then type any additional text in the Find what box.
Type a wildcard character directly in the Find what box.
If you want to replace the item, enter what you want to use as a
replacement in the Replace with box.
Click Find Next, Replace, or Replace All.
To cancel a search in progress, press ESC.

Notes

When the Use wildcards check box is selected, Word finds only the exact
text you specify. (Notice that the Match case and Find whole words only
check boxes are unavailable (dimmed) to indicate that these options are
automatically on; you can¡¯t turn off these options.)
To search for a character that's defined as a wildcard, type a backslash
(\) before the character. For example, type \? to find a question mark.
Wildcards for items you want to find and replace

To find:
Any single character
Type ?
For example, s?t finds "sat" and "set".
Any string of characters
Type *
For example, s*d finds "sad" and "started".
The beginning of a word
Type <
For example, <(inter) finds "interesting" and "intercept", but not
"splintered".

The end of a word
Type >
For example, (in)> finds "in" and "within", but not "interesting".
One of the specified characters
Type [ ]
For example, w[io]n finds "win" and "won".

Any single character in this range
Type [-]
For example, [r-t]ight finds "right" and "sight". Ranges must be in
ascending order.
Any single character except the characters in the range inside the brackets
Type [!x-z]
For example, t[!a-m]ck finds "tock" and "tuck", but not "tack" or "tick".

Exactly n occurrences of the previous character or expression
Type {n}

For example, fe{2}d finds "feed" but not "fed".

At least n occurrences of the previous character or expression
Type {n,}

For example, fe{1,}d finds "fed" and "feed".

From n to m occurrences of the previous character or expression
Type {n,m}

For example, 10{1,3} finds "10", "100", and "1000".

One or more occurrences of the previous character or expression
Type @

For example, lo@t finds "lot" and "loot".

Notes

You can use parentheses to group the wildcard characters and text and to
indicate the order of evaluation. For example, type <(pre)*(ed)> to find
"presorted" and "prevented".
You can use the \n wildcard to search for an expression and then replace it
with the rearranged expression. For example, type (Newton) (Christie) in
the Find what box and \2 \1 in the Replace with box. Word will find "Newton
Christie" and replace it with "Christie Newton".
</quote>



Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Mark,

Thanks for your quickly reply and clarification.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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