I am trying to create a macro that will automatically add auto cross referenced entries (within paragraphs) to documents that have numbered items.
A specific illustration is helpful.
If my document contained legal numbering as follows:
and my selection in the document is the text "This is a sentence referencing paragraph 2.1." I would want my macro (despite the fact that the whole sentence is selected) to only replace the hard text "2.1" with a cross reference entry to the numbered item "2.1".1. This is a paragraph.
2. This is a paragraph.
2.1 This is a paragraph.
3. This is a paragraph.
4. This is a paragraph. This is a sentence referencing paragraph 2.1.
5. This is a paragraph.
I have started with the following code (see below), but it is lacking.
I believe the key portion of the code "str(ExtractNumber(sel.Range, True)) = Left(Trim(v), 1)" is where I am going awry. I am having trouble getting this to evaluate to a true statement.
Specifically, with respect to "str(ExtractNumber(sel.Range, True))" the ExtractNumber function seems to extract decimal numbers from text (ex it will extract the "2.1") but I do not know how to make it extract "legal numbering", for example, if the the text was 2.1.1 it would not work.
With respect to "Trim(v)", I don't know how to extract just the numbered portion.
Would appreciate any guidance on this.
Best,
Brian
[vba]
Sub InsertAutoXRef()
Dim sel As Selection
Dim doc As Document
Dim vHeadings As Variant
Dim v As Variant
Dim i As Integer
Set sel = Selection
Set doc = Selection.Document
' Exit if selection includes multiple paragraphs
If sel.Range.Paragraphs.Count <> 1 Then Exit Sub
' Collapse selection if there are spaces or paragraph marks on either end
sel.MoveStartWhile cset:=(Chr$(32) & Chr$(13)), Count:=sel.Characters.Count
sel.MoveEndWhile cset:=(Chr$(32) & Chr$(13)), Count:=-sel.Characters.Count
vHeadings = doc.GetCrossReferenceItems(wdRefTypeNumberedItem)
i = 1
For Each v In vHeadings
If str(ExtractNumber(sel.Range, True)) = Trim(v) Then
sel.InsertCrossReference _
referencetype:=wdRefTypeNumberedItem, _
referencekind:=wdNumberFullContext, _
referenceitem:=i
Exit Sub
End If
i = i + 1
Next v
MsgBox "Couldn't match: " & sel.Range.Text
End Sub
Function ExtractNumber(rCell As Range, _
Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double
Dim iCount As Integer, i As Integer, iLoop As Integer
Dim sText As String, strNeg As String, strDec As String
Dim lNum As String
Dim vVal, vVal2
''''''''''''''''''''''''''''''''''''''''''
'Extracts a number from a cell containing text and numbers.
'Please note this function is available at: http://www.ozgrid.com/VBA/ExtractNum.htm
'Thanks to the author
''''''''''''''''''''''''''''''''''''''''''
sText = rCell
If Take_decimal = True And Take_negative = True Then
strNeg = "-" 'Negative Sign MUST be before 1st number.
strDec = "."
ElseIf Take_decimal = True And Take_negative = False Then
strNeg = vbNullString
strDec = "."
ElseIf Take_decimal = False And Take_negative = True Then
strNeg = "-"
strDec = vbNullString
End If
iLoop = Len(sText)
For iCount = iLoop To 1 Step -1
vVal = Mid(sText, iCount, 1)
If IsNumeric(vVal) Or vVal = strNeg Or vVal = strDec Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
If IsNumeric(lNum) Then
If CDbl(lNum) < 0 Then Exit For
Else
lNum = Replace(lNum, Left(lNum, 1), "", , 1)
End If
End If
If i = 1 And lNum <> vbNullString Then lNum = CDbl(Mid(lNum, 1, 1))
Next iCount
ExtractNumber = CDbl(lNum)
End Function
[/vba]