We can search some specified text in Microsoft Office Word documents with the help of Find() method, The following example shows how we can search text in document
For it first we need to add reference of Microsoft.Office.Interop.Word library.
We use late binding concept for initializing object of the word application after that we open word document from the specified file path and then we need to clear formatting from previous searches by using the ClearFormatting method prior to the search.
Private Sub FindText() Dim objWordApp As Word.Application objWordApp = CreateObject("Word.Application") ‘objWordApp.Visible = True 'Open an existing document. Dim objDoc As Word.Document = objWordApp.Documents.Open("C:\dfd.doc", ) Dim findText As String = "template" objDoc.Content.Find.ClearFormatting() Try If objDoc.Content.Find.Execute(findText) = True Then MessageBox.Show("Text found.") Else MessageBox.Show("The text could not be found.") End If objDoc.Close() objWordApp.Quit() Catch ex As Exception objDoc.Close() objWordApp.Quit() End Try End Sub
From above we are finding ‘template’ text in the document, code display ‘Text found’ message if we found text otherwise it will display ‘The text could not be found’.
can you explain more about this…what i should put the code on the “button”? to make it more sense to perform the action with clicking the button…