If You want to learn how to find a word or phrase in Word file and get its paragraph or word number stay here, You’ve come to the right place!
Some time ago I wrote about finding phrase in Word or .pdf files, but now we want something more…
Long story short
Recently, I got inspired by topic from StackOverflow, much about finding a word position in the Word file. When I saw this, I wanted to try my skills and create a tool, which finds a word or whole phrase in the Word file and returns its position – number of paragraph or word.
To get things more complicated and be consistent with my website name I wanted to create it in Excel.
Imagine, that in column “A” You store the words, in column “B” locations of Word files You want to search for and in column “C” macro returns words position.
First things first
At the beginning we need to declare variables, which will stand for Word variables: Word application, Word document and Word document ranges.
Dim wd As Word.Application Dim wdDoc As Word.Document Dim rng As Word.Range, rng2 As Word.Range, rParagraphs As Word.Range
Get the search info
After all needed declarations let’s get search phrase srcwd and Word file localization wdfile.
Dim txt As String, wdfile As String, srcwd As String Dim CurPos As Long, GetParNum As Long wdfile = ThisWorkbook.Sheets(1).Range("B1") srcwd = ThisWorkbook.Sheets(1).Range("A1")
Create Word objects
To open Word file from Excel, You need to create Word application object and set its document to next variable.
Set wd = CreateObject("Word.Application") Set wdDoc = wd.Documents.Open(wdfile)
Later on set all Word file content range to rng.
Set rng = wdDoc.Content
Text of this document can be taken out from this range variable.
We got opened target Word file and all of its text assigned to variable. Now we can start to seek for chosen phrase and get its paragraph or word number. To do it we will use function .Find
With rng.Find .Text = srcwd .Forward = True .Execute End With
This will find and select the next occurrence (.Forward = True) of the searched word (srcwd).
Remember about the .Execute, because without it won’t work at all, regardless of parameters number!
Get paragraph number
Finally, we can get the paragraph or word number!
First, we need to set the cursor in the end of the found word. In other case macro will not count the paragraph, which the word is in it!
CurPos = rng.Words(1).End
Next let’s set the range from the beginning of the document to the position of cursor.
Set rParagraphs = wdDoc.Range(Start:=0, End:=CurPos)
Thanks to that we got range containing everything up to searched word and now all we have to do is count the number of paragraphs!
GetParNum = rParagraphs.Paragraphs.Count
If You want to know what is its word number just change Paragraphs to Words.
GetWordsNum = rParagraphs.Words.Count
There is one matter left to do in the end of our code. We have to save somewhere the paragraph number. Let’s write it down to the column C, after that close the document with no changes and quit Word application (see end of below code).
Option Explicit Sub src4Word() Dim wd As Word.Application Dim wdDoc As Word.Document Dim rng As Word.Range, rng2 As Word.Range Dim rParagraphs As Word.Range Dim txt As String, wdfile As String, srcwd As String Dim CurPos As Long, GetParNum As Long wdfile = ThisWorkbook.Sheets(1).Range("B1") srcwd = ThisWorkbook.Sheets(1).Range("A1") Set wd = CreateObject("Word.Application") Set wdDoc = wd.Documents.Open(wdfile) Set rng = wdDoc.Content With rng.Find .Text = srcwd .Forward = True .Execute End With CurPos = rng.Words(1).End Set rParagraphs = wdDoc.Range(Start:=0, End:=CurPos) GetParNum = rParagraphs.Paragraphs.Count ThisWorkbook.Sheets(1).Range("C1") = GetParNum wdDoc.Close 0 wd.Quit End Sub
Now You know how to find a word or phrase in Word file and get its paragraph or even its word number! It looks so easy! You can use this code to the other variations, for example to get whole text of the searched word paragraph. The choice is yours!