Re: Word VBA return location
Hello Andy (and whoever else is interested). I have discovered how to do it!
By way of review, I have searched for and found a word somewhere in my document. Say, for example, it is in paragraph 7 of section 3, but I don't know that yet. I want the program to tell me it is in section 3, paragraph 7.
Andy already showed me how to get the section number (thanks), but none of the suggestions seemed to work for returning the paragraph number. Some of them would return the paragraph number relative to the entire document, but I don't care about that. I only want to know what paragraph it is in this section.
Here is what I have found to work:
Function myReturnParaNum() Dim myRange As Range, myDummy As Integer Set myRange = Selection.Range myDummy = myRange.MoveStart(wdSection, -1) myReturnParaNum = myRange.Paragraphs.Count End Function
This works whether a word is selected in the paragraph or the insertion point is somewhere (anywhere) in the paragraph. The key to it is the "-1" in the count position of the .MoveStart parentheses. This moves the beginning point of the range to the beginning of the section, but does not move the ending point. The .Count method then gives the number of the paragraph in the range, and it doesn't matter if the entire paragraph is part of the range or not.
Thanks again. Even those suggestions which did not work for this particular problem helped me in how to think about it and will no doubt find use in other coding. Charles Thomas