Share via

Macro: MS Word - Highlight words, return page and line number of highlight word.

Anonymous
2018-09-15T01:03:21+00:00

Jay:

I am new to MS Word, having migrated slowly (for almost 2 decades) from WordPerfect.  Needless to say, the transition has not been easy, but is coming along with each day.  Do you have an macro a list a series of words or phrases may be entered, and when ran against a doc it highlights the words or phrases in the Word doc, and displaying on a new page the words or phrases and for each the page number in the doc where it appears, along with the line number?   I will look forward to hearing from you.

Leon Keiter

******@jkkslaw.com

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Anonymous
    2018-09-15T07:24:15+00:00

    An Index, though, won't return the line number...

    True, but using a concordance index best/easiest built in way to collect page numbers for specific words. That list can also be used to highlight the words.  Since the words have also been highlighted using find/replace, then the line numbers are not AS important.

    Take a look at the macros in this document.  It is a free download, with several hundred macros in it we can use or tweak.

    Computer Tools for Editors (and Proofreaders)

    by Paul Beverley, LCGI

    http://www.archivepub.co.uk/book.html

    There are several that do various things with highlighting, specifically "FRedit".  You may be able to use these macros as a starting point for what you want to do.

    ************************

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2018-09-15T02:59:07+00:00

    Hi Leon,

    In addition to the suggestions already made, consider using the Index with a concordance feature.

    http://www.addbalance.com/usersguide/complex_do...

    https://www.youtube.com/watch?v=87S19Xw0clE

    http://www.howtogeek.com/howto/35495/how-to-cre...


    Note: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.


    I hope this information helps. Please let me know if you have any more questions or require further help.

    Regards

    Was this answer helpful?

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2018-09-15T02:29:17+00:00

    If it's a simple list that varies, you might use something like:

    Sub Demo()

    Application.ScreenUpdating = False

    Dim StrList As String, StrFnd As String, StrTmp As String, StrOut As String, i As Long

    StrList = InputBox("Please input the strings to Find, separated by the | character")

    With ActiveDocument

      For i = 0 To UBound(Split(StrList, "|"))

        StrFnd = Trim(Split(StrList, "|")(i))

        If StrFnd <> "" Then

          With .Range

            With .Find

              .ClearFormatting

              .Replacement.ClearFormatting

              .Text = StrFnd

              .Replacement.Text = ""

              .Forward = True

              .Wrap = wdFindStop

              .Format = False

              .MatchCase = False

              .MatchWholeWord = False

              .MatchWildcards = False

              .MatchSoundsLike = False

              .MatchAllWordForms = False

              .Execute

            End With

            Do While .Find.Found

              StrTmp = StrTmp & " " & .Information(wdFirstCharacterLineNumber) & "/" & .Information(wdActiveEndAdjustedPageNumber)

              .Collapse wdCollapseEnd

              .Find.Execute

            Loop

          End With

          StrOut = StrOut & vbCr & StrFnd & ":" & vbTab & Replace(Trim(StrTmp), " ", ", ")

        End If

      Next

      .Range.InsertAfter Chr(12) & StrOut

    End With

    Application.ScreenUpdating = True

    End Sub

    For a static list, you could replace:

    StrList = InputBox("Please input the strings to Find, separated by the | character")

    with something like:

    StrList = "item 1|item 2|etc."

    Beyond that, it's also possible to use lists stored in text files, Word tables, Excel workbooks, etc.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2018-09-15T02:19:58+00:00

    You have my condolences on the migration. I had to do it (forced by work) long ago.  I still MS WP ...

    You can use a macro to do Find/Replace to highlight a list of words.

    You can use the Index feature to generate a list of words and page numbers they occur on.

    The line number part is trickier. It may be able to be done with a macro. I just don't know enough Word macro language to write it, but in general, I think you could do:

    use an input list,

    find the word,

    use the selection. command find the line number,

    use the selection. command to find the page number?

    print word and line number

    repeat find until end of document

    get next word from list

    Here is a macro you can use as a starting point:

    Selecting to the Next Punctuation Mark

    http://wordribbon.tips.net/T012341_Selecting_to_the_Next_Punctuation_Mark.html

    I have this article (no link to it) on what you can do with the Selection. command, including finding line number

    Finding Information about the Selection

    When working with the Selection object, Word provides the Information property to return information that you may need about the object. The general way you use this property is as follows:

    vMyValue = Selection.Information(constant)

    You need to provide, in place of constant, a Word constant that indicates the information you want to access. The Information property can return information of almost any type, which means it can be either numeric or textual. So, unless you know what Word should be returning, it is good to assign the value to a variant.

    The following sections describe the various pieces of information you can get about the selection.

    General Information

    You can find the following general items concerning the selection:

    ·    wdCapsLock. Returns True or False to indicate whether the Caps Lock key is on.

    ·    **wdNumLock.**Returns True or False to indicate whether the Num Lock Key is on.

    ·    wdOverType. Returns True or False to indicate whether Word is operating in Overtype mode or not.

    ·    wdRevisionMarking. Returns True or False to indicate whether Track Changes is turned on.

    ·    wdSelectionMode. Indicate whether the selection is normal (0), a selection in Extend mode (1), or a columnar selection (2).

    ·    wdZoomPercentage. The current zoom setting.

    As an example, you may create a macro that modifies a document extensively. In such a situation you probably wouldn't want to run the macro if Word's Track Changes feature is enabled. This code determines if Track Changes is turned on and only does its work if it is off.

    If Not Selection.Information(wdRevisionMarking) Then

    DoProcessing

    Else

    MsgBox "Please turn off Track Changes and try again."

    End If

    Note that the DoProcessing procedure is called only if the value returned by the Information property is False. If it is True, then a message box is displayed explaining why the processing isn't taking place.

    Selection Information

    You may need to know quite a bit of information about the selection itself. The following pieces of information are available, and they all concern either the selection or the location of the insertion point. (In Word, the insertion point is considered a "collapsed" selection, meaning it is a selection that has no length at all.)

    ·    wdActiveEndAdjustedPageNumber. The page number on which the selection ends.

    ·    wdActiveEndPageNumber. The absolute page number on which the selection ends.

    ·    wdActiveEndSectionNumber. The section number in which the selection ends.

    ·    wdFirstCharacterColumnNumber. How many characters from the left margin the first character of the selection occurs.

    ·    wdFirstCharacterLineNumber. The line number where the first character of the selection occurs on the page where it is located. If operating in Draft, Outline, or Web Layout views, the value returned is -1.

    ·    wdFrameIsSelected. Returns True or False to indicate whether the selection includes a frame or text box.

    ·    wdHeaderFooterType. Indicates whether the selection is in a header or footer and, if so, which header or footer it is in: not in header or footer (-1), even-page header (0), odd-page header (1), even-page footer (2), odd-page footer (3), first-page header (4), or first-page footer (5).

    ·    wdHorizontalPositionRelativeToPage. The number of twips to the selection's left edge from the left edge of the page.

    ·    wdHorizontalPositionRelativetoTextBoundary. The number of twips from the selection's left edge to the left boundary of the text area.

    ·    wdInClipboard. Returns True or False to indicate whether the selection is in the Clipboard pane.

    ·    wdInCommentPane. Returns True or False to indicate whether the selection is in the comment pane.

    ·    wdInEndnote. Returns True or False to indicate whether the selection is in an endnote.

    ·    wdInFootnote. Returns True or False to indicate whether the selection is in a footnote.

    ·    wdInFootnoteEndnotePane. Returns True or False to indicate whether the selection is in the notes pane.

    ·    wdInHeaderFooter. Returns True or False to indicate if the selection is in a header or footer.

    ·    wdInMasterDocument. Returns True or False to indicate if the selection is in a master document that contains at least one subdocument.

    ·    wdInWordMail. Indicates whether the selection is part of a WordMail send note (1), part of a WordMail read note (2), or not in WordMail (0).

    ·    wdNumberOfPagesInDocument. Returns the number of pages in the document.

    ·    wdReferenceOfType. Indicates whether the selection is before a footnote reference (1), before an endnote reference (2), or before a comment reference (3). Returns -1 if the selection includes a reference along with other items or a 0 if the selection is not before a reference.

    ·    wdVerticalPositionRelativeToPage. The number of twips to the selection's top edge from the top edge of the page.

    ·    wdVerticalPositionRelativetoTextBoundary. The number of twips from the selection's top edge to the top boundary of the text area.

    If you are creating macros for others to use, you'll end up using quite a few of the Information property settings in this section. For example, it is common to make sure that the insertion point is within the main body of the document before your perform some tasks, such as inserting new text. The way you do this is to check to see if the selection is in some special area of the document, such as a header or a footnote:

    Dim bOK As Boolean

    bNotOK = Selection.Information(wdInHeaderFooter)

    bNotOK = bNotOK Or Selection.Information(wdInCommentPane)

    bNotOK = bNotOK Or Selection.Information(wdInEndnote)

    bNotOK = bNotOK Or Selection.Information(wdInFootnote)

    bNotOK = bNotOK Or Selection.Information(wdFrameIsSelected)

    bNotOK = bNotOK Or Selection.Information(wdInFootnoteEndnotePane)

    bNotOK = bNotOK Or Selection.Information(wdInHeaderFooter)

    If bNotOK Then

    MsgBox "Please move to the main document body and try again."

    Else

    DoProcessing

    End If

    Word has many "me too" / copycat features they long ago added to "be the same" as WP.  Line numbers is one of them. They are a very superficial feature.

    **Adding Line Numbers**

    http://wordribbon.tips.net/T008795_Adding_Line_Numbers.html

    Many types of documents, such as legal documents, require the use of line numbers to make it easier to identify specific locations in the document. Here’s how you can add line numbers to your documents.

    Part 1 Line Numbering 

    https://davescomputertips.com/how-to-create-technical-documents-in-word-part-i/

    Creating technical documents may require more formatting than the documents most folks work on. Microsoft Word though, provides the tools necessary to make creating technical documents very easy. Below, I will cover some of the elements common to technical documents and the features you can use to create them.

    This tip gives an idea of how you can set things up to do it, but it is not pretty

    **Cross-Referencing to Line Numbers******http://word.tips.net/T000281_Cross-Referencing_to_Line_Numbers.html

    Wouldn’t it be nice to be able to do cross-referencing to line numbers within a document? Word doesn’t have a built in way to do it, but the workaround presented in this tip may provide the cross-references in a limited setting.

    Jumping to a Line Number

    http://word.tips.net/T001166_Jumping_to_a_Line_Number.html

    Need to jump to a specific line number in your document? It’s easy to do using the Go To command, as described in this tip

    Was this answer helpful?

    0 comments No comments