Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-18-2022, 01:26 AM
jec1 jec1 is offline XRef macro Windows 10 XRef macro Office 2019
Advanced Beginner
XRef macro
 
Join Date: Jan 2012
Posts: 84
jec1 is on a distinguished road
Default XRef macro

Hi all, I thought this macro worked on the type of document I have attached for cross-references. But it errors. I was hoping it would xref up to 4 levels 1.1(a)(i)(A).



Any assistance most appreciated.

Code:
Sub InsertAutoXRefsTest()
'Macropod
Application.ScreenUpdating = False
Dim Doc As Document, Para As Paragraph
Dim ListNums As String, StrNum As String
Dim i As Long, j As Long, x As Long
Set Doc = ActiveDocument: ListNums = "|"
With Doc
  For Each Para In .Paragraphs
    With Para.Range.ListFormat
      If .ListString <> "" Then ListNums = ListNums & .ListString & "|"
    End With
  Next
  For i = 1 To UBound(Split(ListNums, "|")) - 1
    x = Len(Split(ListNums, "|")(i))
    If x > j Then j = x
  Next
  Do While j > 0
    For i = UBound(Split(ListNums, "|")) - 1 To 1 Step -1
      StrNum = Split(ListNums, "|")(i): x = Len(StrNum)         'Subscript out of rangee
      If x = j Then
        ListNums = Replace(ListNums, "|" & StrNum & "|", "|")
        Call MakeAutoXRefs(Doc, StrNum)
      End If
    Next
    j = j - 1
  Loop
End With
Application.ScreenUpdating = True
End Sub


Sub MakeAutoXRefs(Doc As Document, StrNum As String)
Dim RefList As Variant, i As Long, j As Long
With Doc
  RefList = .GetCrossReferenceItems(wdRefTypeNumberedItem)
   
  
  For i = 1 To UBound(RefList)
  
    If Split(Trim(RefList(i)), " ")(0) = StrNum Then
    j = i: Exit For
    End If
  Next
  If j = 0 Then Exit Sub
  With .Range
    With .Find
      .ClearFormatting
      .replacement.ClearFormatting
      .text = " " & StrNum
      .replacement.text = ""
      .Format = False
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
    End With
    Do While .Find.Execute
      If .Fields.Count = 0 Then
        .Start = .Start + 1
        .InsertCrossReference ReferenceType:="Numbered item", _
          ReferenceKind:=wdNumberFullContext, ReferenceItem:=j, _
          InsertAsHyperlink:=True, IncludePosition:=False, _
          SeparateNumbers:=False, SeparatorString:=" "
        .End = .End + 1
        .End = .Fields(1).result.End
      End If
      .Collapse wdCollapseEnd
    Loop
  End With
End With
End Sub
Attached Files
File Type: docx xRef Test 18.2.22.docx (103.0 KB, 11 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Remove Paras with Line Spac 6; Macro to Convert Paragraphs to Outline Numbered venganewt Word VBA 0 01-25-2022 06:28 PM
Field, XRef, Link, Mail Merge? Not sure what I need. - way to duplicate info elsewhere jafpcu Word 2 01-28-2021 01:26 PM
XRef macro xref to heading 5.1 stubbornly displays 0 as heading number eNGiNe Word 4 06-17-2018 11:05 PM
Spell check macro within macro button field doesn't work in one document samuelle Word VBA 0 07-20-2016 02:27 AM
Macro Needed to bold specific lines and Macro to turn into CSV anewteacher Word VBA 1 05-28-2014 03:59 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft