Word VBA return location

  • I have a Word document with several sections. Each section has several paragraphs. After I search for and find a particular word in the entire document, how can I get the program to return the location (section number and paragraph number) in which the word was found? Thanks.

  • Re: Word VBA return location


    Hi,


    You can use the Information property to get section and line number.
    [vba]' active section
    selection.Information(wdActiveEndSectionNumber)
    ' active line number
    Selection.Information(wdFirstCharacterLineNumber)[/vba]

  • Re: Word VBA return location


    Thanks, Andy. Now I think I am halfway home. Your code works, and the first part, to return the section number, is very helpful. The second part, to return the line number, probably will help me with something in the future, but it does not help with the current problem of returning the paragraph number. (It would be great if all my paragraphs were exactly one line long.) Thanks again. CT

  • Re: Word VBA return location


    Found these methods to return more information.
    [vba]ActiveDocument.Range(0, Selection.Range.End).Words.Count
    ActiveDocument.Range(0, Selection.Range.End).paragraphs.Count
    ActiveDocument.Range(0, Selection.Range.End).Sections.Count
    ActiveDocument.Range(0, Selection.Range.End).Sentences.Count[/vba]

  • Re: Word VBA return location


    Quote from Andy Pope

    Found these methods to return more information.
    [vba]ActiveDocument.Range(0, Selection.Range.End).Words.Count
    ActiveDocument.Range(0, Selection.Range.End).paragraphs.Count
    ActiveDocument.Range(0, Selection.Range.End).Sections.Count
    ActiveDocument.Range(0, Selection.Range.End).Sentences.Count[/vba]


    Hey, Andy. This looks promising. Thanks. I will let you know if it solves my problem after I have a chance to go home and try it (tomorrow).

  • 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:

    Code
    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

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!