0

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!

Mazepaad
  • 9
  • 4
  • 1
    Please edit your question to include the actual code you tried, and explain exactly what the problem was when you tried running it. It's difficult from your post to tell exactly what it is you want to do. – Tim Williams Aug 29 '23 at 17:58
  • `For Each w In objWordApplication.ActiveDocument.Words` – taller_ExcelHome Aug 29 '23 at 18:42
  • It doesn't seem to throw an error, but won't go to the next word either. Unsure how to edit my question, but can post a couple more lines, if I knew how to edit the question. Kind of new to this platform. Thx – Mazepaad Aug 30 '23 at 14:26
  • @TimWilliams Unsure how to edit my question, but can post a couple more lines, if I knew how to edit the question. Kind of new to this platform. Thx – Mazepaad Aug 30 '23 at 14:32
  • @TimWilliams Do For Each w In wordapp.ActiveDocument.Words .Application.Selection.Find.Text = Range("A2") If Range("A2") <> w Then .Application.Selection.Find.Execute .Application.Selection = Range("A3") .Application.Selection.Find.Text = Range("A2") .Save End If Next w Loop Until .Application.Selection.Find.found = False – Mazepaad Aug 30 '23 at 14:44
  • There's an edit link right below your question: https://stackoverflow.com/posts/77002559/edit In addition to adding your code it would be helpful to explain what you're trying to do. – Tim Williams Aug 30 '23 at 15:16
  • @TimWilliams Thx, please find edits above. – Mazepaad Aug 30 '23 at 15:58
  • "it simply opens, finds, replaces, closes, the first instance" you didn't include any code related to opening or closing the document. – Tim Williams Aug 30 '23 at 16:04
  • @TimWilliams Should be updated now. – Mazepaad Aug 30 '23 at 17:41
  • Here's an example of replacing all instances of a word with another https://stackoverflow.com/a/21449819/478884 – Tim Williams Aug 30 '23 at 17:52
  • @TimWilliams Thanks, will try part of this out. If I come up with a solution, will post and thnaks again! – Mazepaad Aug 30 '23 at 18:17

0 Answers0