MS Word - Reference to highest number in a numbered list?
Here is a macro to do what you ask. This solution is less prone to being destroyed during document editing.
- Create a Bookmark called
MyList
on the "1." of the list you want to count. (See selection in image.) - Create a reference to to
ListCount
for the result
Sub ListCountMacro()
For i = 1 To Lists.Count
If Lists(i).Range.Start = Bookmarks("MyList").Start Then Exit For
Next i
c = ActiveDocument.Lists(i).CountNumberedItems
For Each aVar In ActiveDocument.Variables
If aVar.Name = "ListCount" Then Num = aVar.Index
Next aVar
If Num = 0 Then
ActiveDocument.Variables.Add Name:="ListCount", Value:=c
Else
ActiveDocument.Variables(Num).Value = c
End If
Selection.WholeStory
Selection.Fields.Update
End Sub
You will have to run the macro each time you wish to update ListCount, so this is still not fully automatic. However, you could make the macro run whenever the document is opened, see here
I don't think word provides this as a feature, the closest you can get is to reference "numbered" items, but adding new items to the end of the list will leave these pointing at the old end (ie, they don't update as you want them to).
However, I have a potential work-around for you: add a bookmark to a "hidden" line in the last item of the list, and reference that. It's not perfect, but might help. Please note that I am using Word 2003, but the instructions should hopefully be similar for 2007 as well.
Place your cursor at the end of the last line of text in your list and press Shift+Enter.
This will insert a line break (as opposed to a normal paragraph break), essentially adding a new line to the current row, but importantly without adding a new list number. You might also want to turn on the paragraph markers when doing this to see what is happening.With your cursor on this new line, use Tools -> Bookmarks and create a new bookmark. If you don't select any text first the bookmark will be invisible, which is probably what you want.
To create a reference, use Tools -> Reference -> Cross-reference.., select Bookmark as the Reference Type and make sure you set the Insert Reference To field to be Paragraph Number.
To extend this list so the last number is updated, you simply add a standard line return at the end of the last element (ie, you must insert new lines before the bookmark line). This way the bookmark is always part of the last "paragraph" in the list thanks to it's line break, so references should be numbered correctly.
Issues
- If you add new line breaks in the wrong place (ie, after the bookmark) you list will break and if you're not careful fixing it you'll move the bookmark and mess up your references.
- You end up with an "empty" line after your list.
Try this:
- Add a Bookmark to some text (doesn't matter which) in the final item of the list, and name it
LastStep
. - Go to the beginning of the list and type: "The last step ... is step"
-
Insert a Field with this code*:
{ REF LastStep \n }
This should add a bookmark reference to the bookmark named LastStep
, and instead of including the bookmark text (which is the default), it gives the paragraph number.
*Alternatively, use the menus like this (Word 2007): Insert > Quick Parts > Field > Ref > LastStep > "Paragraph # from marked paragraph" > OK
Note: When you add additional items before the last step, the number won't update instantly on screen. Right-click on the field and choose Update Field