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

Microsoft Word OLE Visual Basic 6

Status
Not open for further replies.

cj92713696

Programmer
Nov 23, 2000
105
US
I am trying to do something that seems very simple in nature, yet overpowering via code! =) Your help is very much appreciated!

1.) I need Visual Basic to open an existing Microsoft Word document. I have no problems with this part. ;)
2.) I then need Visual Basic to go through the entire loaded document to replace text. If I have an occurence of <<customer>> I'd like to replace that with a customer name via Visual Basic code. As of the moment I'm using the words.item object, but it's really slow. I really need to find out how to use the replace command but have no clue. I am asking for any help you may offer... Thanks a lot!
 
Hi

Try this code (I don't know execution times though)
Maybe you can compare it to your code's execution time
Code:
Option Explicit

Private x As Word.Application
Private doc As Word.Document
Private Sub Command1_Click()

   With doc.Content.Find
      .Execute  _
              FindText:=txtFind.Text, _
              ReplaceWith:=txtReplace.Text, _
              Format:=True, Replace:=wdReplaceAll
   End With

   doc.Save

End Sub
Private Sub Form_Load()

   Set x = CreateObject(&quot;Word.Application&quot;)
   Set doc = x.Documents.Open(&quot;d:\template.dot&quot;)

End Sub
Private Sub Form_Unload(Cancel As Integer)

   x.Quit
   Set x = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top