View Single Post
 
Old 10-20-2019, 04:50 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

FWIW, I use a macro to create cross-references to help reduce the inadvertent breaking caused by the mechanics of how cross-refs work. I find this is faster and works IMHO better in a number of ways.
1. I can see the bookmarks inserted via the macro (if I set my options to see bookmarks) so that I am less likely to make the mistake of inserting a new paragraph from the start of a bookmarked para.
2. I don't need to use the Insert Cross References dialog, change type and scroll to find an entry that is most likely already visible on my screen.
3. I can reinstate bookmarks in the GUI if they have been removed inadvertently. (The GUI on current versions of Word won't let you (re)create a bookmark name that starts with "_" although you could do this in older versions of Word or via VBA)
4. When adding a cross-ref to a paragraph number, I can strategically place these bookmarks NOT at the start of a paragraph to allow me to actually add paragraphs in the way I have been advising you not to. The cross-ref to a para number doesn't care if the bookmark isn't the first thing in a paragraph so I can position the bookmark somewhere after the first character for safety.

The following macro works differently depending on whether you select text or just position your cursor in a paragraph. Either way, it creates a cross-ref and stores it in the clipboard so you can then paste the cross-ref in one (or more) locations.
Code:
Sub MakeXRef()
  'Creates two different types of cross references depending on selection  
  Dim sRef As String, aXRef As Field, aRng As Range, sFieldCode As String
  Dim aRng2 As Range, aBkmk As Bookmark, sPrefix As String
  
  Set aRng = Selection.Range
  'Check to see if we can reuse an existing bookmark or need to create a new one
  For Each aBkmk In aRng.Bookmarks
    If aBkmk.Range = aRng Then
      sRef = aBkmk.Name
      Exit For
    End If
  Next aBkmk
  If sRef = "" Then sRef = "xRef" & Format(Now(), "yyyymmddhhMMss")
  ActiveDocument.Bookmarks.Add sRef, aRng
  
  'Create the XRef and add it immediately after the selection
  If Len(aRng.Text) > 0 Then
    sFieldCode = "Ref " & sRef & " \h"
    sPrefix = ""
  Else
    sFieldCode = "Ref " & sRef & " \h \n"
    sPrefix = "Section "
  End If
  Set aRng2 = aRng
  aRng2.Collapse Direction:=wdCollapseEnd
  Set aXRef = ActiveDocument.Fields.Add(aRng2, Text:=sFieldCode, PreserveFormatting:=False)
  aXRef.Select
  Set aRng = Selection.Range
  aRng.InsertBefore sPrefix
  aRng.Cut
  
  StatusBar = "Cross-reference copied to the clipboard. Paste away..."
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote