Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2021, 08:12 AM
gdsgssy gdsgssy is offline Insert cross reference like copy and paste Windows 10 Insert cross reference like copy and paste Office 2019
Novice
Insert cross reference like copy and paste
 
Join Date: May 2021
Posts: 2
gdsgssy is on a distinguished road
Default Insert cross reference like copy and paste


I made a tool called HandyRef with VBA that provides a handy way to insert Cross Reference in MS Word, just like copy and paste. "Copy" what you want to reference and "paste" it at the point where cross reference is needed. HandyRef abandons the long list for selecting reference items that MS Word uses and greatly speeds up the cross-reference process. HandyRef provides Ribbon and keyboard shortcut for the user. Picture below shows the Ribbon that integrated in MS Word "Reference" Ribbon tab. By the way, HandyRef is open-source. Details and code can be found at Github
Reply With Quote
  #2  
Old 05-19-2021, 04:36 PM
Guessed's Avatar
Guessed Guessed is offline Insert cross reference like copy and paste Windows 10 Insert cross reference like copy and paste Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
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

Your code is similar in principle to how I've done it on my templates. So I will suggest a couple of other things you may not have considered yet.

In our documents, often we want to refer to the heading outline number rather than the text. So when the selection.range is empty, rather than producing a warning to first select text, I create an xRef to the paragraph number (by including '\n' in the ref field code) and add a prefix (eg. Section X)

Secondly, when testing to see if a bookmark already exists in the same range, you don't need to compare it to every bookmark in the document - you should compare it to every bookmark that included that range.
Code:
    'search for existing bookmark reference the same range
    'doc.Bookmarks.ShowHidden = True  'this isn't needed for the loop
    For Each bmi In rg.Bookmarks
        If bmi.Range.IsEqual(rg) Then
            Set oldbm = bmi
            Exit For
        End If
    Next bmi
    'doc.Bookmarks.ShowHidden = False  'this can annoy users who want it on
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-19-2021, 07:25 PM
gdsgssy gdsgssy is offline Insert cross reference like copy and paste Windows 10 Insert cross reference like copy and paste Office 2019
Novice
Insert cross reference like copy and paste
 
Join Date: May 2021
Posts: 2
gdsgssy is on a distinguished road
Default

Thank you for you suggestion. I have changed the code to add bookmarks into range instead of document. To keep it simple, I don't want to add the function inserting reference to heading outline number right now. Thank you again.
Code:
    Dim oldbm As Bookmark
    Dim bmi As Bookmark
    Dim bmShowHiddenOld As Boolean
    bmShowHiddenOld = rg.Bookmarks.ShowHidden
    
    'search for existing bookmark reference the same range
    rg.Bookmarks.ShowHidden = True
    For Each bmi In rg.Bookmarks
        If bmi.Range.IsEqual(rg) And bmi.Name Like BookmarkPrefix & "#*" Then
            Set oldbm = bmi
            Exit For
        End If
    Next bmi
    rg.Bookmarks.ShowHidden = bmShowHiddenOld
Reply With Quote
Reply

Tags
cross reference

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert cross reference like copy and paste Direct (copy-paste) reference from another paragraph (CITATION) WordNoobx Word 6 05-15-2018 05:46 AM
Insert cross reference like copy and paste insert cross-reference – why is a level 2 style not a heading? eNGiNe Word 8 01-30-2018 06:00 AM
Insert cross reference like copy and paste Find {text} and insert cross reference from bookmark Slamzor Word VBA 1 12-01-2017 05:12 PM
Insert cross reference like copy and paste Insert a cross-reference in a caption RomFigo Word 9 09-25-2015 12:32 AM
Can you copy & paste cells across worksheets and preserve reference to worksheet? New Daddy Excel 2 11-27-2013 07:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:36 PM.


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