![]() |
|
|||||||
|
|
Thread Tools | Display Modes |
|
#10
|
||||
|
||||
|
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 |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Highlight numbers after a specific word in numbered list
|
liblikas90 | Word VBA | 3 | 02-27-2019 03:52 AM |
Problem with bold numbers in multi-level list styles when having numbered headings AND paragraphs
|
bwofficer | Word | 2 | 12-12-2014 12:21 AM |
| Numbers in Numbered List Insist on Being Bold | peytontodd | Word | 2 | 09-19-2014 11:01 AM |
| Word Mixing Numbered Headings with Numbered List | Tess0 | Word | 11 | 07-15-2014 05:25 AM |
| Auto-Update Amended Page Numbers? | herbertp | Word | 5 | 02-01-2013 02:20 AM |