Wildcards in obj.ActiveDocument.Content.Find.Execute

  • Thread starter Georgios Liakopoulos
  • Start date
G

Georgios Liakopoulos

Hi,
I find and replace text in a .doc file using MS Access visual basic.
Here is my Sub:

***************************************************************
Public Sub ReplaceText(obj As Word.Application, vSource As String, vDest
As String)
'Replace all occurences of vSource with vDest in Word doc
obj.ActiveDocument.Content.Find.Execute FindText:=vSource, _
ReplaceWith:=vDest, Format:=True, _
Replace:=wdReplaceAll
End Sub
***************************************************************

I pass through the vSource string using another sub like:
*****************************************************
.. . .
Dim objWord As Word.Application
.. . .
Set objWord = New Word.Application 'create instance of Word
objWord.Documents.Add "C:\Documents and
Settings\Labuser\Desktop\AddRefs.doc" 'open document (vFilename)
objWord.ScreenUpdating = False 'turn off screen updates
.. . .

If ActiveDocument.Content.Find.Execute(FindText:="FIXED
STRING") = True Then
.. . .
ReplaceText objWord, "FOUND FIXED STRING"
.. . .
****************************************************

My Question:
Can I search text using wildcards?, like:

****************************************************
If ActiveDocument.Content.Find.Execute(FindText:="(PRODUCT CODE: "*")")
= True Then
*****************************************************

Thank you for reading
Georgios
 
D

David Horowitz

Georgios,
Yes, there are many wildcard options for Find/Replace in Word.
I would suggest that you open up Word, hit Ctrl-H (or Edit > Replace (or
Home > Editing > Replace in 2007) and play around with the dialog box.
If you hit the More button in the Replace dialog, you'll see a checkbox for
"Use wildcards". Then if you hit the Special button, you'll see all kinds of
wildcards you can use.
This means, in your code, you must use MatchWildcards:=True in your Execute
call
Also, it seems to me that there is no benefit to first searching, then
replacing, when you can do it all in one action. I would just remove the
first call to FInd, and just always call ReplaceText.
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
Georgios Liakopoulos said:
Hi,
I find and replace text in a .doc file using MS Access visual basic. Here
is my Sub:

***************************************************************
Public Sub ReplaceText(obj As Word.Application, vSource As String, vDest
As String)
'Replace all occurences of vSource with vDest in Word doc
obj.ActiveDocument.Content.Find.Execute FindText:=vSource, _
 
D

Doug Robbins - Word MVP on news.microsoft.com

See the article "Finding and replacing characters using wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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