Finding the line number in Word
Quick question - how do I find the line number a particular word is on?
Code:
For iCount = 1 To ThisDocument.Content.Words.Count
sWord = Trim(ThisDocument.Content.Words.Item(iCount))
if sWord = <criteria> then
iLineNumber = ?????
end if
Next iCount
Thanks in advance
Re: Finding the line number in Word
AFAIK, there is no line number as word wrapping and margins will throw off the count when opened under different systems.
What are you trying to do?
Re: Finding the line number in Word
Here's a simple subroutine to report both the page number and the line number. You need both to identify your position in the document because line numbers are numbered relative to the current page.
Code:
Sub j()
Dim getPage As Integer, lnNum As Integer
getPage = Selection.Information(wdActiveEndPageNumber)
lnNum = Selection.Information(wdFirstCharacterLineNumber)
Debug.Print "Reported Page: " & getPage
Debug.Print "Reported Line: " & lnNum
End Sub
Now it's your turn: :) -- show me how to add rows to an existing form in wordBasic. -- I'd be VERY grateful -- no response to my thread so far :(
Bob.
P.S. Of course, as your previous respondent pointed out, any time you change fonts or margins prior to the current position these values will change.
B.
Re: Finding the line number in Word
Nice :) I must have missed the Information function.
Learned something new today :thumb:
Re: Finding the line number in Word
Thanks for that, it's a very handy function but I'm not sure I can use it for my current situation.
What I'm trying to do is this; I have a form, on which is a listview filled with a number of words from the document that meet a certain criteria, along with their word number.
When I click on the word in the listview, I want the document to go to and highlight the word I clicked on the listview. I hope that explains it ok.
James
Re: Finding the line number in Word
Ok, then you will want to use the .Find method which you can have highlight its matches.
Re: Finding the line number in Word
You may be able to get the word count for a selection by highlighting the document up to the word of interest, then using the wordCount function -- I'm not at work today and don't have word 2003 at home, so I can't give the specifics -- Anybody know how to add a label and textbox to a MsWord 2003 form in VBA?
Bob.
Re: Finding the line number in Word
Record a macro of you doing the task. Then view the module generated code.