This little bit of code in Word inserts a cross reference to the paragraph number of the paragraph where the cursor is located. The cross reference is bracketed by some fixed text, so that the result is:
[CDRL crossref]
Then it marks that text as a TOA entry.
It works, but as you can see it's nasty in it's use of selection.extend and selection.typetext.
Any pointers on cleaning this up to use Range rather than selection would be appreciated.
[CDRL crossref]
Then it marks that text as a TOA entry.
It works, but as you can see it's nasty in it's use of selection.extend and selection.typetext.
Code:
Public Sub InsertCDRL()
Dim CurrentPosition As String
CurrentPosition = Selection.Range.ListFormat.ListString
myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
Application.ScreenUpdating = False
For i = 1 To UBound(myHeadings)
If InStr(LCase$(myHeadings(i)), CurrentPosition & " ") Then
Selection.TypeText Text:="[CDRL "
Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberRelativeContext, ReferenceItem:=CStr(i), _
InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, _
SeparatorString:=" "
Selection.TypeText Text:="]"
Selection.MoveLeft unit:=wdSentence, Count:=1
Selection.Extend ("]")
ActiveWindow.ActivePane.View.ShowAll = True
ActiveDocument.TablesOfAuthorities.MarkCitation Range:=Selection.Range, _
ShortCitation:="[CDRL " & CurrentPosition & "]", LongCitation:="[CDRL " & CurrentPosition & "]", _
LongCitationAutoText:="", Category:=1
ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View. _
ShowAll
End If
Next i
Application.ScreenUpdating = True
End Sub
Any pointers on cleaning this up to use Range rather than selection would be appreciated.