VBA Find Text in Word Doc in Excel doesn't work - stuck


good people

This is half the build process, sorry if confused

I have this code where I define a dictionary in excel. From there, I want to find text from "keywords" in the Word document, and once found, I want to move on to other encodings.

The problem is, I only get to this .findpart, I can't work on it for the rest of my life, why it can't find anything.

Attention is drawn to the following:

For Each Key In Dict

After that, all I'm asking is to find the text in the string C. I know the fact that Ccontains a value because I added a MsgBoxcheck and added it to the clipboard as well, so I can try to find the text manually - if I can search manually, so can

However, when running/stepping through the code, the command .find.executeseems to be ignored as if it's not even trying to search for the Document, and blnFoundevery time Boolean returns False, jumps to Next. At that time my screen also showed the document (opened by the code) but nothing happened.

Can someone tell me what I am doing wrong here? I am totally confused.

Thanks!

Sub FindReplaceInWord2()

    Dim Wbk As Workbook: Set Wbk = ThisWorkbook
    Dim Wrd As New Word.Application
    Dim Dict As Object
    Dim RefList As Range, RefElem As Range
    Dim A As String
    Dim B As String
    Dim C As String
    Dim test As New DataObject
    Dim blnFound As Boolean    

    Wrd.Visible = True

    Dim TokenDoc As Document
    Set TokenDoc = Wrd.Documents.Open("\\SERVER\Client\Table.dot")        

    Set Dict = CreateObject("Scripting.Dictionary")
    Set RefList = Wbk.Sheets("Sheet1").Range("A1:A236") 

    With Dict
        For Each RefElem In RefList
        On Error Resume Next
            If Not .Exists(RefElem) And Not IsEmpty(RefElem) Then
            A = RefElem.Value
           .Add RefElem.Value, RefElem.Offset(0, 1).Value
            B = RefElem.Value

              End If
        Next RefElem
    End With

    For Each Key In Dict

    Set test = New DataObject
    'MsgBox Key
    test.SetText (Key)
    test.PutInClipboard
    C = Key
    MsgBox C
     With Wrd.ActiveDocument.Find

     .Text = C

    End With
   blnFound =  Wrd.ActiveDocument.Find.Execute        

        If blnFound = True Then
            MsgBox = "Yay for working it out"
            Else
        MsgBox = "Boo, it didn't Work"
        End If
    Next Key      

End Sub

PS. I also tried

   Wrd.Selection.Find.text = C
   blnFound = Wrd.Selection.Find.Execute

and add it before the lookup

   TokenDoc.Activate
Siddharth Rout

Is this what you want to try ( tested on local template files )

Sub FindReplaceInWord2()
    Dim Wbk As Workbook: Set Wbk = ThisWorkbook
    Dim RefList As Range, RefElem As Range

    Dim col As New Collection
    Dim itm

    Dim blnFound As Boolean

    Dim Wrd As New Word.Application
    Dim TokenDoc As Document

    Wrd.Visible = True

    'Set TokenDoc = Wrd.Documents.Open("D:\Users\SidzPc\Desktop\Temp\Table.dot")
    Set TokenDoc = Wrd.Documents.Open("\\SERVER\Client\Table.dot")

    Set RefList = Wbk.Sheets("Sheet1").Range("A1:A236")

    For Each RefElem In RefList
        On Error Resume Next
        col.Add RefElem.Value, CStr(RefElem.Value)
        On Error GoTo 0
    Next RefElem

    For Each itm In col
        With Wrd.Selection.Find
            .Text = itm
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

        blnFound = Wrd.Selection.Find.Execute

        If blnFound = True Then
            MsgBox "Yay for working it out"
        Else
            MsgBox "Boo, it didn't Work"
        End If
    Next itm
End Sub

Related


VBA Find Text in Word Doc in Excel doesn't work - stuck

good people This is half the build process, sorry if confused I have this code where I define a dictionary in excel. From there, I want to find the text from the "keywords" in the Word document, and once found, I want to move on to other encodings. The problem

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

find text and replace text excel word vba

rakesh seebaruth I have the following code Sub DocSearch() Dim wdApp As Object, wdDoc As Object Set wdApp = CreateObject("word.application") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-

VBA copy text from table in word doc to excel?

User 7415328 I have a word document: Table 1 A A B B Table 2 C C D D I'm trying to copy each piece of text from a table in a word document into a cell in excel like this: Spreadsheets: Column A Column B A A B

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

Find and replace text in excel vba in word document

James Treyton I'm trying to create multiple word documents based on one template, currently I can open the template word doc and save it as the filename I want, which is pulled from a table in excel. What I want to do is to replace the text "##Title##" in the

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

Find word in long text in cell (Excel VBA)

Israel I'm trying to develop a "find" button that will be able to mark the words contained in a cell with a red "all". For example if I have this text in my cell. "Pepper has mint in his pocket" should change the font like this. " Pepper has peppermint in his

vba: Can't use offset in word script in excel doc

Robock I have a Word document and I should open an Excel document, find the first empty cell in a range, and start filling certain cells with information. Before this I wanted to use 'offset', but for some reason it gave 'offset' error. Here is the relevant pa

vba: Can't use offset in word script in excel doc

Robock I have a Word document and I should open an Excel document, find the first empty cell in a range, and start filling certain cells with information. Before this I wanted to use 'offset', but for some reason it gave 'offset' error. Here is the relevant pa

vba: Can't use offset in word script in excel doc

Robock I have a Word document and I should open an Excel document, find the first empty cell in a range, and start filling certain cells with information. Before this I wanted to use 'offset', but for some reason it gave 'offset' error. Here is the relevant pa

Why doesn't PHPword setvalue replace text in word Doc?

Abdul Aziz Cadmore I am trying to edit a word document using php ( PHPword library) . However, I'm not sure why it doesn't work. The script did not throw any errors. my code: <?php require 'vendor/autoload.php'; // $phpword = new \PhpOffice\P

Word replacement macro in Excel doesn't work

Paul I am trying to enter Excel results into a pre-made Word document. I want to use macros for this. I'm pretty new to coding games, but I know some basics. I tried copying from different classes, but none of them worked. This simple macro is supposed to chan

Find longest word in string doesn't work

Peter Utkar I am supposed to find the longest word in the string, here is the code I have come up with so far. Unfortunately this doesn't seem to work, my question is why? function findLongestWordLength(str) { str.split(""); let longest = 1; for(let i =

Find longest word in string doesn't work

Peter Utkar I am supposed to find the longest word in the string, here is the code I have come up with so far. Unfortunately this doesn't seem to work, my question is why? function findLongestWordLength(str) { str.split(""); let longest = 1; for(let i =

Find longest word in string doesn't work

Peter Utkar I am supposed to find the longest word in the string, here is the code I have come up with so far. Unfortunately this doesn't seem to work, my question is why? function findLongestWordLength(str) { str.split(""); let longest = 1; for(let i =