Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access97 automating Find and Replace in Word97 doc 1

Status
Not open for further replies.

tunsarod

Programmer
Oct 31, 2001
294
GB
This issue has been discussed before but I'm raising it again since I haven't yet found a real solution.

I have a regular need to remove extraneous text from a comma delimited text file prior to importing the data, which often contains occassional asterisks and quotation marks in divers places. I have a Word macro which removes these quite efficiently but I would very much like to have this problem solved within an Access module that accomplishes the same result without having to call the Macro.

I have searched the Microsoft KB, Microsoft Automation 97 Help file, Access VBA Help file, Word VBA help file. Many methods are suggested such as this found in the Object browser under Word,Range,Find:

This example replaces "Hello" with "Goodbye" in the active document.

Set myRange = ActiveDocument.Content
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Hello"
.Replacement.Text = "Goodbye"
.Execute Replace:=wdReplaceAll
End With

and this simple offering also found via the Word 8 object library :

This example finds all occurrences of the word "hi" in the active document and replaces each occurrence with "hello."

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="hi", ReplaceWith:="hello", Replace:=wdReplaceAll

Both of these and several others I had a go with failed. Most caused a Fatal Error message and Access closed. In particular Access had no truck with the line .Clearformatting

If anyone has a neat and reliable VBA solution for finding and removing specified text that does not involve running a Word macro from Access I would be very interested.

Rod


 
As long as you have the word 8 object library referenced in your references then this should work:


Sub WordReplace(MyDoc As String, MyFind As String, MyReplace As String)

Dim objWord As Word.Application
Set objWord = CreateObject("word.application")

objWord.Visible = True
objWord.Documents.Open (MyDoc)

With objWord.Selection.Find
.text = MyFind
.ClearFormatting ' ****This line is optional
.Replacement.text = MyReplace
.Replacement.ClearFormatting ' ****This line is optional
.Execute replace:=wdReplaceAll, Forward:=True
End With

Set objWord = Nothing

End Sub
 
Thanks folks.

I will check these out and report back as to any success.

Rod
 
Parax,

Well I tried your method on my laptop and it failed causing a fatal exception. So I tried it on another machine and it worked!!!

So what's up with my laptop? Dunno but its either one of the Reference files or the Access exe itself I guess.

I don't know why I didn't try it on the desktop machine before.

Anyway, thanks again.

Rod

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GHolden,

Thanks for that address. Quite a bit of bedtime reading methinks. All 35 pages of it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top