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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation - Find Text and make it Italic 3

Status
Not open for further replies.

wbwillson

Technical User
Oct 7, 2002
52
GB
I've created a word doc with bookmarks that I use to populate from my tables. I need to be able to search for certain words after the merge has occured and then make those words Italic. I've tried the following code:

Dim objWord As Word.Application

Set objWord = CreateObject ("Word.Application")

With objWord
.visible = True
.Documents.Open ("C:\test.doc")

.Selection.Find.ClearFormatting
.Selection.Find.Replacement.ClearFormatting
'Italics Code
.Selection.Find.Replacement.Font.Italic = True

With Selection.Find
.Text = "italic"
.Replacement.Text = "Found"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End With


This code doesn't work though...any ideas what I'm doing wrong?

Bill
 
Try this:

Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
With objWord
.Visible = True
.Documents.Open ("C:\test.doc")

.Selection.Find.ClearFormatting
.Selection.Find.Replacement.ClearFormatting
'Italics Code
.Selection.Find.Replacement.Font.Italic = True

With Selection.Find
.Text = "italic"
.Replacement.Text = "Found"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
Set objWord = Nothing


Let me know how you get on.
 
Billpower,

That worked perfectly! Guess I just needed the
Selection.Find.Execute Replace:=wdReplaceAll

Thanks for your help!

Bill
 
Billpower,

the code works, but I've discovered that when I run the code the first time it works fine, when I then close the word doc, and try it again I get the following error:

Run-Time error '462':
The remote server machine does not exist or is unavailable

the debugger then points to

With Selection.Find

in the code. If I hit Stop on the debugger and run the code again it works again. Always crashes on the second try. What is best error handler for this? I've tried using a RESUME NEXT line in the error handler and this stops me getting an error message but it also doesn't find and italicize my text. Any ideas? Thanks again

Bill
 
One thing i've noticed, if I run the code it works. Then if I close word, and goto the VB editor and press stop on the debug bar, go back to form and run it again it works every time. So I think we just need to figure a way to stop the debugger after it executes the code. Maybe switching the focus, or calling another function? What do you think?

Bill
 
Hi wbwillson, I'm still working on this problem, I've tried so many different things, including your ideas, to no avail. Will let you know if I come up with a solution. Today has not been a very good day.

It's a bit raw though that Microsoft's own "Fix" doesn't work.
 
billpower,

Yes, it is a bit frustrating! I thought perhaps the RESET command might do the trick, but I tried it with no luck.

Bill
 
billpower,

regarding our run-time error with automation problem.

here is what i did, and it seems to be going well (so far):

in the load event of the form:
Set wdApp = New Word.Application


in the UnLoad event of the form:
wdApp.Quit
Set wdApp = Nothing

Let me know if this works for you.

Bill
 
Hi Bill,

Sorry for the delay in replying, I tried the above but it didn't seem to make any difference, still getting error 462, in fact I've tried what feels like a million ways to get this working, but to no avail. Have you made any progress since?

Bill
 
For what it's worth.. I did some word automation a few years back and took a look at the code.

The only significant difference between the two approaches was that I used a Word.Document object

e.g.
dim objDoc As Word.Document
Set objDoc = objWord.Documents.Add(SourceDocName)

and did all operations using the objDoc object

Then, when done with the document,

objDoc.Close False
Set objDoc = Nothing

I had any of the problems you've mentioned, perhaps this will make a difference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top