Find/Replace only supports alphabetic characters?

D

Dave

I'm trying to do a find/replace by automating Word from a C# application.
It works fine as long as my find/replace text is alphabetic. If I try to do
a find/replace using a string such as "C#", I get an error: "The Find What
text for a Find All Word Forms search can only contain alphabetic letters.".
I tried enclosing the string in quotes, but that doesn't work either. That
actually will cause the problem with text that otherwise works - it doesn't
like the quotes either.

Here's my code (this code highlights all instances of "text" in red/bold):
Object oReplaceAll =
Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
Object oText = text;
Object oReplaceText = text;
wordDoc.Select();
msWord.Selection.Find.Text = oText.ToString();
msWord.Selection.Find.Replacement.Text = oReplaceText.ToString();
msWord.Selection.Find.Replacement.Font.Bold = 1;
msWord.Selection.Find.Replacement.Font.Color =
Microsoft.Office.Interop.Word.WdColor.wdColorRed;
msWord.Selection.Find.Execute(ref oText, ref oFalse, ref oFalse, ref
oFalse, ref oFalse, ref oTrue, ref oTrue, ref oFalse, ref oTrue, ref
oReplaceText, ref oReplaceAll, ref oMissing, ref oMissing, ref oMissing, ref
oMissing);


You can do what I want from within word itself - how do you call
find/replace and allow non-alphabetic characters?
- Dave
 
H

Helmut Weber

Hi Dave,
it is probably the option MatchAllWordForms,
or did you learn at elementary school any
words that contain a #-sign? ;-)
Remember my ResetSearch sub?
Should precede and close all search operations.
Sub Resetsearch()
With Selection.find
.Parent.Collapse
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

Dave

Awesome, Helmut - that did it! Thanks.

This code is the back end to support a query form from which people enter
the strings. I hadn't given much thought to "MatchAllWords", other than a
brief notion that it would be useful when I first wrote the code, but didn't
consider that it would cause a problem like this. I changed the code to
only turn on MatchAllWords if the string is alphabetic.

And - greetings to Bavaria from Boston! I was just there over the summer.
Beautiful area.

- Dave
 

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