Answers for "word vba find a text in a word document"

VBA
0

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:TestACBS.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
Posted by: Guest on April-04-2020

Code answers related to "word vba find a text in a word document"

Code answers related to "VBA"

Browse Popular Code Answers by Language