In other case macro will not count the paragraph, which the word is in it! CurPos = rng.Words(1).End Execute, because without it won’t work at all, regardless of parameters number! Get paragraph numberįinally, we can get the paragraph or word number!įirst, we need to set the cursor in the end of the found word. Forward = True) of the searched word ( srcwd).
This will find and select the next occurrence (. Now we can start to seek for chosen phrase and get its paragraph or word number. We got opened target Word file and all of its text assigned to variable. Text of this document can be taken out from this range variable. Later on set all Word file content range to rng. Set wdDoc = wd.Documents.Open(wdfile) Assign content Set wd = CreateObject("Word.Application") To open Word file from Excel, You need to create Word application object and set its document to next variable. Srcwd = ThisWorkbook.Sheets(1).Range("A1") Create Word objects Wdfile = ThisWorkbook.Sheets(1).Range("B1") Dim txt As String, wdfile As String, srcwd As String Dim wd As Word.Applicationĭim rng As Word.Range, rng2 As Word.Range, rParagraphs As Word.Range Get the search infoĪfter all needed declarations let’s get search phrase srcwd and Word file localization wdfile. First things firstĪt the beginning we need to declare variables, which will stand for Word variables: Word application, Word document and Word document ranges. 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. To get things more complicated and be consistent with my website name I wanted to create it in Excel. 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. Recently, I got inspired by topic from StackOverflow, much about finding a word position in the Word file.
#VBA WORD BUILD RANGE BY PARAGRAPH PDF#
pdf files, but now we want something more… Long story short Some time ago I wrote about finding phrase in Word or.