Use excel macro to find text in .docx file

Slade2000

Board Regular
Joined
Feb 3, 2009
Messages
118
Hi

I am looking for a macro that will take the value of a cell "A1" and search for it in a .docx file and then just add the word "Yes" into cell "B1"

This code is what i have and it opens the word doc but then i need to perform the find.

Thank you

Code:
Sub FindName()
     
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.docx")
Dim FindWord As String
FindWord = Sheet1.Range("A1").Value
MsgBox FindWord
************
Search the word and set "B1" to "Yes"

************
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Give this a try. I was able to get the desired results in all my test.


Code:
Sub FindName()     
    Dim wrdApp As Object
    Dim wrdDoc As Object
    Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.docx")
    Dim FindWord As String
    Dim result As String
    FindWord = Sheet1.Range("A1").Value
   ' MsgBox FindWord
    
    '// Defines selection for Word's find function
    wrdDoc.SelectAllEditableRanges
    
    '// Word Find Method Setup Block
    With wrdDoc.ActiveWindow.Selection.Find
        .Text = FindWord
        .Replacement.Text = ""
        .Forward = True
        .Wrap = 1 ' wdFindContinue (Word constant not defined in Excel)
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    '// Execute find method selects the found text if found
    wrdDoc.ActiveWindow.Selection.Find.Execute
    
    '// Store Selected text
    result = wrdDoc.ActiveWindow.Selection.Text
    
    '// Test if the selection string found match value required converted case for comparison
    If UCase(result) = UCase(FindWord) Then
        Sheet1.Range("B1").Value = "YES"
    Else
        Sheet1.Range("B1").Value = "NO"
    End If
    '// Close and don't save application
    wrdApp.Quit SaveChanges:=0 ' wdDoNotSaveChanges (Word constant not defined in Excel)
    
End Sub
 
Upvote 0
Another method...

Code:
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.docx")
    Dim FindWord As String
    FindWord = Sheet1.Range("A1").Value
    MsgBox FindWord
    
    wrdApp.Selection.WholeStory
    wrdApp.Selection.Find.ClearFormatting
    With wrdApp.Selection.Find
        
        .Text = FindWord
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        
        If .Execute Then
            Sheet1.Range("B1").Value = "Yes"
        Else
            Sheet1.Range("B1").Value = "No"
        End If
        
    End With
 
Last edited:
Upvote 0
Ralajer yours work perfectly. The only problem i have now is that some have suffices and starts with the same and although the text does not exist in the document it tell exel that it does. For instance

ACBS_EOD - This one does not exist
ACBS_EOD_SA - This one does exist

And still it will return that both do exist. Is there a way to find exact text?
 
Upvote 0
That will be an easy fix change the MatchWholeWord = False to = True. AlphaFrog included that in his code which is probably a little better than mine because I bet that AlphaFrog knows Word Object Library a little/a lot better than me.
 
Upvote 0
I have used both pieces of code and changed the WholeWord to true but still get the same result.

Sub FindText2()

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.docx")
Dim FindWord As String
For i = 2 To 27
FindWord = Sheet1.Range("A" & i).Value
'MsgBox FindWord

wrdApp.Selection.WholeStory
wrdApp.Selection.Find.ClearFormatting
With wrdApp.Selection.Find

.Text = FindWord
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

If .Execute Then
Sheet1.Range("B" & i).Value = "Yes"
Else
Sheet1.Range("B" & i).Value = "No"
End If

End With
Next
End Sub
 
Upvote 0
I tried the code that you submitted and I am not having the partial word match problem that you seem to be experiencing. I not sure why you are you are having that issue it might have something to do with the docx that is being searched.
 
Upvote 0
I have created a plan .dox file now with the same and got the same results. The red is what i have problems with.

This is the excel entries

ACBS_DCI_ArchiveNo
ACBS_Run_BackupNo
LDN_ACBS_BIM_AlertNo
ACBS_EOD_DummyNo
ACBS_EOD
Yes
ACBS_DatamartYes
Sbyn_ACBSTransferDoneDatNo
Sbyn_ACBSTransferOthersNo
ACBS_Interface_ExtractsNo
ACBS_EOD_SA_DummyNo
ACBS_EOD_SA
Yes
Sbyn_ACBSSATransferDoneDatNo
ACBS_SA_DatamartNo
ACBS_SA_Interface_ExtractsNo
Funding_Recon_SANo
F_RECON_TRANSFERNo
ACBS_SA_STATEMENTS_XMLNo
ADS_HOLIDAY_FILENo
ACBS_SA_STATEMENTS_BATCHYes
ACBS_EOD_SF_DummyNo
Sbyn_ACBSSFLTransferDoneDatNo
Sbyn_ACBSSFLTransferOthersNo
ACBS_SF_DatamartNo
ACBS_SF_Interfaces_ExtractsNo
ACBS_EOD_SF
No
ACBS_Intraday

This is my .doc entries

ACBS_EOD_SA
ACBS_SA_STATEMENTS_BATCH
ACBS_Datamart

No





<colgroup><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
This removes the underscores in the word doc and from the "search-for" term. Then does the search.

Code:
    [COLOR=darkblue]Dim[/COLOR] wrdApp [COLOR=darkblue]As[/COLOR] Word.Application
    [COLOR=darkblue]Dim[/COLOR] wrdDoc [COLOR=darkblue]As[/COLOR] Word.Document
    [COLOR=darkblue]Set[/COLOR] wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = [COLOR=darkblue]True[/COLOR]
    [COLOR=darkblue]Set[/COLOR] wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.doc")
    [COLOR=darkblue]Dim[/COLOR] FindWord [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    FindWord = Sheet1.Range("A1").Value
    MsgBox FindWord
    
    [COLOR=darkblue]With[/COLOR] wrdDoc.Content.Find
        .ClearFormatting
        .Forward = [COLOR=darkblue]True[/COLOR]
        .Wrap = wdFindContinue
        .Format = [COLOR=darkblue]False[/COLOR]
        .MatchCase = [COLOR=darkblue]False[/COLOR]
        .MatchWholeWord = [COLOR=darkblue]True[/COLOR]
        .MatchWildcards = [COLOR=darkblue]False[/COLOR]
        .MatchSoundsLike = [COLOR=darkblue]False[/COLOR]
        .MatchAllWordForms = [COLOR=darkblue]False[/COLOR]
        
        [COLOR=green]'Remove underscores from word document text[/COLOR]
        .Execute FindText:="_", ReplaceWith:="", Replace:=wdReplaceAll
        
        .Text = Replace(FindWord, "_", "") [COLOR=green]'Remove underscores from "Search-for" text[/COLOR]
        [COLOR=darkblue]If[/COLOR] .Execute [COLOR=darkblue]Then[/COLOR]
            Sheet1.Range("B1").Value = "Yes"
        [COLOR=darkblue]Else[/COLOR]
            Sheet1.Range("B1").Value = "No"
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
Upvote 0
AplhaDog i tried that but as well did not work so i went this way and now it works fine.

Code:
Sub FindText2()

    Cells.Replace What:="_", Replacement:="", LookAt:=xlPart, SearchOrder:= _
    xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:\Test\ACBS.docx")
    Dim FindWord As String
    'MsgBox FindWord
    
    wrdApp.Selection.WholeStory
    wrdApp.Selection.Find.ClearFormatting
    
    With wrdApp.Selection.Find
        .Execute FindText:="_", ReplaceWith:="", Replace:=wdReplaceAll
        .Text = Replace(FindWord, "_", "")
    End With
    
    For i = 2 To 27
    FindWord = Sheet1.Range("A" & i).Value
    
    With wrdApp.Selection.Find
      
        .Text = FindWord
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        
        If .Execute Then
            Sheet1.Range("B" & i).Value = "Yes"
        Else
            Sheet1.Range("B" & i).Value = "No"
        End If
          
    End With
    Next
    wrdApp.Quit SaveChanges:=0 ' wdDoNotSaveChanges (Word constant not defined in Excel)

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,685
Members
449,329
Latest member
tommyarra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top