user9749094
0
Q:

excel vba open word document and find text

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
		.style= ("choose your style to look for")
    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
0

New to Communities?

Join the community