Word Index Refer to Numbered List not Page

Anonymous
2018-12-31T03:02:04+00:00

I am asking this question, I don't think it is possible.  This is a 2 step question, assuming that the first one is not possible. 

first question

============

I have a document with multiple pages.  Where, the document is made of numbered list for example

  1. word 1, word 2, word 3 etc.
  2. apple, orange, goat
  3. table, chair, mouse
  4. computer, printer, floor

I am building an index, 

A

apple, 2

O

orange, 2

T

table, 3

as you can see rather refer to page number, the index will refer to the list instead.  I cannot find a way to do that or whether it can be done by macro.

2nd question, this should have been another question, but I put it here instead

===================================================

assuming that question number 1 is not possible, then 

I was looking the following article, try this macro but return the wrong number

https://wordmvp.com/FAQs/Numbering/ListString.htm

all what I need to do, from the list below

  1. word 1, word 2, word 3 etc.
  2. apple, orange, goat
  3. table, chair, mouse
  4. computer, printer, floor

run a macro to get the list number.  for example, if I select "word 3" and run the macro, it will return "1"

as well as if I select "mouse" and run the macro it will return "3"

this is the simplest way, if 1 is not possible 

Moved from: Office/ Word / Windows 10 / Office 2016

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
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-11T00:06:13+00:00

The code I posted inserts a PAGE field rather than just a page number. That approach allows for the possibility subsequent edits will result in the indexed content appearing on a different page.

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-08T23:12:18+00:00

I don't get any such error when running the code. Are you using it exactly as posted and selecting the content to be indexed?

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-25T23:03:30+00:00

I did say that would happen... See my reply of January 15.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-13T05:06:49+00:00

but that special character still shows up in the index when I insert the index at the end.  

That was the intention. The non-breaking space doesn't print and, if you click on the ¶ symbol on the Ribbon's home tab, the symbol that looks like º will disappear.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-13T05:04:06+00:00

I think I can simply forget about the page number and rely with the paragraph number, since the paragraphs are numbered, so there is not need to go to specific page number

…

I think I should just forget about the page numbers.

Do be aware that, with the \t switch (which is what you need to report the paragraph #), the entries will be reported will be reported in descending order:

Apple. 2, 1

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-12T07:15:55+00:00

Try:

Sub Demo()

Application.ScreenUpdating = False

ActiveWindow.View.ShowFieldCodes = True

With Selection

  ActiveDocument.Indexes.MarkEntry Range:=.Range, _

    Entry:=.Text & " PL", EntryAutoText:=.Text, Bold:=False, Italic:=False

  .End = .End + 1

  With .Fields(1).Code

    .End = .End - 2

    .Start = .End - 2

    .Font.Bold = True

    .Collapse wdCollapseEnd

    .InsertBefore Chr(160)

    .Font.Bold = False

    .Collapse wdCollapseEnd

    .Fields.Add Range:=.Duplicate, Type:=wdFieldEmpty, _

      PreserveFormatting:=False, _

      Text:="STYLEREF ""Default Paragraph Font"" \n"

  End With

End With

ActiveWindow.View.ShowFieldCodes = False

Application.ScreenUpdating = True

End Sub

This will produce:

Apple PL ‎1, 1

Apple PL ‎2, 2

Apple PL ‎3, 3

Apple PL ‎4, 5

Apple PL ‎5, 8

where the number after PL is the paragraph # and the number after the comma is the page #.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-01-11T22:48:41+00:00

That is not how an Index works. What you'd ordinarily get is something like:

A

Apple, 2, 3, 4, 5

where the numbers are page or paragraph references. To get what you want, every XE field would have to be unique, perhaps by including the PL & paragraph # in the XE field's argument, rather than as a switch.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2019-02-28T05:56:15+00:00

Try:

Sub Demo()

Application.ScreenUpdating = False

Dim Fld As Field, strnum As String

For Each Fld In ActiveDocument.Fields

  With Fld

    If .Type = wdFieldIndexEntry Then

      Do While .Code.Characters.Last = " "

        .Code.Characters.Last = vbNullString

      Loop

      Do While .Code.Characters.Last.Previous = "."

        .Code.Characters.Last.Previous = vbNullString

      Loop

    End If

  End With

Next

Application.ScreenUpdating = True

End Sub

Was this answer helpful?

0 comments No comments

51 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-01-10T22:55:19+00:00

    I haven't tried your code above, but I think it may be possible to add the page number.  

    by running your macro, I get something like that

    Apple{XE "Apple" 2}

    where 2 is the paragraph number.  It would have been better to modify the macro to put the paragraph number inside the quote for example

    Apple {XE "Apple 2"}

    Now when I run the insert index, then it will add the page automatically.  I don't know anyVBA, but if you capture the selection like

    TextSelectA as a variable then PagraphNumber as another variable 

    then it can be something like 

    TextSelecdt A = TextSelectA + "PL" + PagraphNumber 

    where PL is mu tag.  The PL is common for the whole documents or all fields 

    when I insert it, I can have 

    A

    Apple PL 2, PageNumber

    I will try your code later

    Was this answer helpful?

    0 comments No comments