Find/Replace MSWord wildcard characters from within Excel

S

Susan

Excel 2003, Windows XP

This code, based in an Excel workbook, retrieves words from Microsoft Word (Word 2003). But first it strips out all punctuation. I'm having trouble stripping out the wildcard characters.

This is the code fragment not working:

Set oRange = ActiveDocument.Range
With oRange.Find
.Text = "?"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
End With
oRange.Find.Execute Replace:=wdReplaceAll


I've tried replacing
.Text = "?"
with
.Text = "/?"

which is the MSWord signal to look for the actual character mark, and

.Text = "~?"

which is the Excel signal to look for the actual character mark, but neither works.

Any ideas? I also need this to find the MSWord wildcard characters ! and *.

Thank you!
Susan
 
G

GS

I use the following function whenever I need to filter unwanted
characters from strings. Perhaps you will find it helpful...

Function FilterString$(ByVal TextIn As String, _
Optional IncludeChars As String, _
Optional IncludeLetters As Boolean = True, _
Optional IncludeNumbers As Boolean = True)
' Filters out all unwanted characters in a string.
' Arguments: TextIn The string being filtered.
' IncludeChars [Optional] Keeps any characters.
' IncludeLetters [Optional] Keeps any letters.
' IncludeNumbers [Optional] Keeps any numbers.
'
' Returns: String containing only the wanted characters.

Const sSource As String = "FilterString()"

'The basic characters to always keep
Const sLetters As String = "abcdefghijklmnopqrstuvwxyz"
Const sNumbers As String = "0123456789"

Dim i As Long, CharsToKeep As String

CharsToKeep = IncludeChars
If IncludeLetters Then _
CharsToKeep = CharsToKeep & sLetters & UCase(sLetters)
If IncludeNumbers Then CharsToKeep = CharsToKeep & sNumbers

For i = 1 To Len(TextIn)
If InStr(CharsToKeep, Mid$(TextIn, i, 1)) Then _
FilterString = FilterString & Mid$(TextIn, i, 1)
Next
End Function 'FilterString()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

You're welcome!

Not that there are some characters you'll want to include that occur at
the end of a sentance or paragraph as valid punctuation. These should
be included in the string passed for IncludeChars...

"!.? "

...where you'll also include any other special characters. You'll need
to modify the loop to include a check that the question character
(which may be a wildcard) is not valid punctuation...

For i = 1 To Len(TextIn)
If Mid$(TextIn, i, 2) = "? " Or _
Mid$(TextIn, i, 3) = "?" & vbCrLf Then
FilterString = FilterString & "~"
Else
If InStr(CharsToKeep, Mid$(TextIn, i, 1)) Then _
FilterString = FilterString & Mid$(TextIn, i, 1)
End If
Next
FilterString = Replace(FilterString, "~", "?")

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

Typo!
You're welcome!
Note
that there are some characters you'll want to include that occur
at the end of a sentance or paragraph as valid punctuation. These
should be included in the string passed for IncludeChars...

"!.? "

..where you'll also include any other special characters. You'll need
to modify the loop to include a check that the question character
(which may be a wildcard) is not valid punctuation...

For i = 1 To Len(TextIn)
If Mid$(TextIn, i, 2) = "? " Or _
Mid$(TextIn, i, 3) = "?" & vbCrLf Then
FilterString = FilterString & "~"
Else
If InStr(CharsToKeep, Mid$(TextIn, i, 1)) Then _
FilterString = FilterString & Mid$(TextIn, i, 1)
End If
Next
FilterString = Replace(FilterString, "~", "?")

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
S

Susan

Thanks again, Garry! I did keep your helpful code on hand, but for me, this time, I decided it was easier to use the ASCII codes for the particular characters in the Word document (^nnn where "nnn" is the ASCII code) in the Find/Replace coding. It works perfectly.

In the grand scheme of things, your code probably works better, but I already had it set up a certain way and this worked. Thank you again for your help!

Warmly,
Susan
 
G

GS

Thanks again, Garry! I did keep your helpful code on hand, but for
me, this time, I decided it was easier to use the ASCII codes for the
particular characters in the Word document (^nnn where "nnn" is the
ASCII code) in the Find/Replace coding. It works perfectly.

In the grand scheme of things, your code probably works better, but I
already had it set up a certain way and this worked. Thank you again
for your help!

Warmly,
Susan

Thanks for the feedback! ..much appreciated!

Best wishes in your endeavors...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 

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