Wanting to write on a Word document after looping through the words in the document already. If anyone could recommend what objects need to be included and how to start this For
loop, would be appreciated!
For Each word In WordDoc
doesn't seem to be accepted and gives an error. I've activated my Word document and am trying to loop through the words.
I've tried:
For Each w In objWordApplication.ActiveDocument.Words
As well as, below:
'Variables including word application, word doc, document, and argument for column on worksheet.
Public wordapp
Public WordDoc
Public document
Public Arr(1) As Variant
'A procedure for opening an excel document and finding and replacing a specific word in the document.
Sub OpenDocFromExcel()
Dim wordapp
Dim strFile As String
Dim WordDoc
Dim word
'Create the word application and open a word doc.
strFile = Range("A1")
Set wordapp = CreateObject("word.application")
Set WordDoc = wordapp.Documents.Open(strFile)
wordapp.Visible = True
WordDoc.Activate
intCounter = 1
'WordDoc.Content.InsertAfter ("This is a test.")
'Select text in the word doc and replace text in the word doc.
With WordDoc
Do
.Application.Selection.Find.Text = Range("A2")
.Replacement.Text = Range("A3")
.Application.Selection.Find.Execute
.Collapse
.Extend
.Application.Selection = Range("A3")
.Application.Selection.Find.Text = Range("A2")
.Save
.Application.Selection.Find.Execute
Loop Until .Application.Selection.Find.Found = False
End With
'End With
'Close the word application.
With wordapp
.Quit
End With
End Sub
I am trying to find and replace all instances of a word in a word doc with opening and closing the application so I can execute the program against a number of files quickly. With the code, above, it won't move to the next instance, it simply opens, finds, replaces, closes, the first instance.
Thanks in advance for any help!