none
Word VBA/Macro for Indexing a document RRS feed

  • Question

  • In Word, Search and replace offers the option to FIND NEXT and then optionally replace the Target.

    When Marking a word or phrase for Indexing, the only choice is MARK or MARK ALL.

    Would it be possible to write a Macro that allowed you to select the term to be indexed, found each instant and then allowed the user to call (or not, as the case may be) the INDEX dialogue to MARK it?

    As an example, if I have an article that references Wordsworth, but sometimes it is William and sometimes Dorothy, I cannot select Wordsworth and MARK ALL - but marking each one individually is very time-consuming.

    Any suggestions would be gratefully received.

    Rodney.

    Monday, December 2, 2013 4:52 PM

Answers

  • You could use a macro like:

    Sub IndexBuilder()
    Application.ScreenUpdating = False
    Dim StrIdx As String, IdxStr As String
    StrIdx = InputBox("What is the reference to Index" & vbCr & "e.g. William Wordsworth", "Index Builder")
    If StrIdx = vbNullString Then Exit Sub
    IdxStr = Split(StrIdx, " ")(1) & ":" & Split(StrIdx, " ")(0)
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = StrIdx
        .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
        ActiveDocument.Indexes.MarkEntry Range:=.Duplicate, Entry:=IdxStr, _
        EntryAutoText:=IdxStr, CrossReference:="", CrossReferenceAutoText:="", _
        BookmarkName:="", Bold:=False, Italic:=False, Reading:=""
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    With the above code, you'd do one search for William Wordsworth and another for Dorothy Wordsworth. both would be listed separately in the index under Wordsworth.

    Note: As coded, the macro only deals with two-word names.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 3, 2013 5:56 AM
  • Did you input the name as indicated (e.g. William Wordsworth)? Do bear in mind that, as coded, the macro will only find strings that match your input. Also, unless you have Word configured to display hidden text, you won't see the added Index fields. Finally, the code doesn't update the actual Index - you can do that by selecting it and pressing F9.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 3, 2013 9:06 PM

All replies

  • Hi,

    This is the forum to discuss questions and feedback for Microsoft Office, I'll move your question to the MSDN forum for Word

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=worddev&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    George Zhao
    TechNet Community Support

    Tuesday, December 3, 2013 3:11 AM
  • You could use a macro like:

    Sub IndexBuilder()
    Application.ScreenUpdating = False
    Dim StrIdx As String, IdxStr As String
    StrIdx = InputBox("What is the reference to Index" & vbCr & "e.g. William Wordsworth", "Index Builder")
    If StrIdx = vbNullString Then Exit Sub
    IdxStr = Split(StrIdx, " ")(1) & ":" & Split(StrIdx, " ")(0)
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = StrIdx
        .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
        ActiveDocument.Indexes.MarkEntry Range:=.Duplicate, Entry:=IdxStr, _
        EntryAutoText:=IdxStr, CrossReference:="", CrossReferenceAutoText:="", _
        BookmarkName:="", Bold:=False, Italic:=False, Reading:=""
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    With the above code, you'd do one search for William Wordsworth and another for Dorothy Wordsworth. both would be listed separately in the index under Wordsworth.

    Note: As coded, the macro only deals with two-word names.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 3, 2013 5:56 AM
  • Thanks Paul.

    I will give that a try.

    Rodney

    Tuesday, December 3, 2013 1:54 PM
  • Paul

    I have tried the code.

    It prompts me for an entry, but then appears to do nothing.

    Am I doing something wrong?

    Best regards

    Rodney

    Tuesday, December 3, 2013 4:33 PM
  • Did you input the name as indicated (e.g. William Wordsworth)? Do bear in mind that, as coded, the macro will only find strings that match your input. Also, unless you have Word configured to display hidden text, you won't see the added Index fields. Finally, the code doesn't update the actual Index - you can do that by selecting it and pressing F9.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 3, 2013 9:06 PM