Referencing list item relative to this one

D

drmrbrewer

What I'm trying to do is to create a function that (when assigned to a
keyborad shortcut) allows me to insert a quick reference back to the
previous item in a numbered list.

So, say I've got:

1. Some stuff here
2. Some more stuff
3. Here I refer back to the stuff in 2.

So, when I'm typing in item 3, I just hit a keyboard shortcut to get
the "2" reference there.

What I've done so far is along the lines of:

Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext,
ReferenceItem:=Selection.Range.ListFormat.ListValue - 1,
InsertAsHyperlink _
:=True, IncludePosition:=False

This actually works fine. UNLESS (and it's a big UNLESS) there is
another numbered list in my document before the one I'm working on.
Because that upsets the numbering.... it seems that "ListValue" just
returns the actual number of the current list item (3 in the example
above), and when that value (or 3 - 1 = 2 calculated from it) is set
as the ReferenceItem, it is a interpreted as a number applied across
the GLOBAL set of numbered items.

Example:

1. An annoying list here that messes my method up

[Some more stuff here]

1. Some stuff here
2. Some more stuff
3. Here I want to refer back to the previous item, but it comes out as
1.

When I try my cross-reference method in item 3, it works out "3 - 1 =
2", and then applies that to the global list of numbered items, and
no. 2 in that list is actually item 1 of the second list, not item 2
as I want.

Any idea as to how to make this work even when I have multiple
numbered groups in my document?

Thanks!!

Mike
 
R

Russ

Mike,
Try this for Reference Item:
ReferenceItem:=UBound(ActiveDocument.GetCrossReferenceItems(wdRefTypeNumbere
dItem)) - 1

GetCrossRefenceItems normally returns an array of available items of that
type that can be cross referenced. In your scenario, the cursor would be in
the last one of that list. Without actually storing that array list in a
variable, we feed it to the Ubound function, which returns the highest
element number of an array (or count, if the array elements start from 1 and
not 0). Then we subtract one from that count to relatively refer the next to
the last item in the list of available items.

What I'm trying to do is to create a function that (when assigned to a
keyborad shortcut) allows me to insert a quick reference back to the
previous item in a numbered list.

So, say I've got:

1. Some stuff here
2. Some more stuff
3. Here I refer back to the stuff in 2.

So, when I'm typing in item 3, I just hit a keyboard shortcut to get
the "2" reference there.

What I've done so far is along the lines of:

Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext,
ReferenceItem:=Selection.Range.ListFormat.ListValue - 1,
InsertAsHyperlink _
:=True, IncludePosition:=False

This actually works fine. UNLESS (and it's a big UNLESS) there is
another numbered list in my document before the one I'm working on.
Because that upsets the numbering.... it seems that "ListValue" just
returns the actual number of the current list item (3 in the example
above), and when that value (or 3 - 1 = 2 calculated from it) is set
as the ReferenceItem, it is a interpreted as a number applied across
the GLOBAL set of numbered items.

Example:

1. An annoying list here that messes my method up

[Some more stuff here]

1. Some stuff here
2. Some more stuff
3. Here I want to refer back to the previous item, but it comes out as
1.

When I try my cross-reference method in item 3, it works out "3 - 1 =
2", and then applies that to the global list of numbered items, and
no. 2 in that list is actually item 1 of the second list, not item 2
as I want.

Any idea as to how to make this work even when I have multiple
numbered groups in my document?

Thanks!!

Mike
 
R

Russ

Mike,
Things get much more complicated when you are inserting into a previously
defined list or if you are not in the very last item of things to
crossreference. Then I would suggest popping up the dialog box and choosing
what you want to reference.

With Dialogs(wdDialogInsertCrossReference)
.Update
.InsertAsHyperlink = 1
.Show
End With


Mike,
Try this for Reference Item:
ReferenceItem:=UBound(ActiveDocument.GetCrossReferenceItems(wdRefTypeNumbere
dItem)) - 1

GetCrossRefenceItems normally returns an array of available items of that
type that can be cross referenced. In your scenario, the cursor would be in
the last one of that list. Without actually storing that array list in a
variable, we feed it to the Ubound function, which returns the highest
element number of an array (or count, if the array elements start from 1 and
not 0). Then we subtract one from that count to relatively refer the next to
the last item in the list of available items.

What I'm trying to do is to create a function that (when assigned to a
keyborad shortcut) allows me to insert a quick reference back to the
previous item in a numbered list.

So, say I've got:

1. Some stuff here
2. Some more stuff
3. Here I refer back to the stuff in 2.

So, when I'm typing in item 3, I just hit a keyboard shortcut to get
the "2" reference there.

What I've done so far is along the lines of:

Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext,
ReferenceItem:=Selection.Range.ListFormat.ListValue - 1,
InsertAsHyperlink _
:=True, IncludePosition:=False

This actually works fine. UNLESS (and it's a big UNLESS) there is
another numbered list in my document before the one I'm working on.
Because that upsets the numbering.... it seems that "ListValue" just
returns the actual number of the current list item (3 in the example
above), and when that value (or 3 - 1 = 2 calculated from it) is set
as the ReferenceItem, it is a interpreted as a number applied across
the GLOBAL set of numbered items.

Example:

1. An annoying list here that messes my method up

[Some more stuff here]

1. Some stuff here
2. Some more stuff
3. Here I want to refer back to the previous item, but it comes out as
1.

When I try my cross-reference method in item 3, it works out "3 - 1 =
2", and then applies that to the global list of numbered items, and
no. 2 in that list is actually item 1 of the second list, not item 2
as I want.

Any idea as to how to make this work even when I have multiple
numbered groups in my document?

Thanks!!

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top