Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-23-2021, 05:50 PM
Guessed's Avatar
Guessed Guessed is offline Placing numbers of cross-references of tables and figures inside parenthesis Windows 10 Placing numbers of cross-references of tables and figures inside parenthesis Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

This appears to work with your sample docs. I would recommend testing it on A COPY of your real documents.
Code:
Sub RestrictXRefs()
  Dim aFld As Field, aRngXRef As Range, sWord As String, aRngAnchor As Range, sBkmk As String
  Dim arrCode() As String, aRng As Range
  For Each aFld In ActiveDocument.Fields
    If aFld.Type = wdFieldRef Then
      Set aRngXRef = aFld.Result
      sWord = LCase(Trim(aRngXRef.Words(1)))
      If sWord = "table" Or sWord = "figure" Then
        arrCode = Split(Trim(aFld.Code), " ")
        sBkmk = arrCode(1)
        If ActiveDocument.Bookmarks.Exists(sBkmk) Then
          Set aRngAnchor = ActiveDocument.Bookmarks(sBkmk).Range
          aRngAnchor.MoveStart Unit:=wdCharacter, Count:=Len(sWord) + 1
          ActiveDocument.Bookmarks.Add Name:=sBkmk, Range:=aRngAnchor
          aFld.Update
          aFld.Select
          Selection.Range.InsertBefore sWord & " ("
          Selection.Range.InsertAfter ")"
        End If
      End If
    End If
  Next aFld
End Sub
I'm happy to accept criticism and corrections to the Selection object I used. I was having trouble with the Range object insertbefore and insertafter (kept going inside the field result) so I took the unfortunate step of using Selection to get around that issue. This won't hurt your functionality but isn't the way I would prefer to do it.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #2  
Old 07-23-2021, 10:36 PM
laith93 laith93 is offline Placing numbers of cross-references of tables and figures inside parenthesis Windows 10 Placing numbers of cross-references of tables and figures inside parenthesis Office 2019
Competent Performer
Placing numbers of cross-references of tables and figures inside parenthesis
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Smile

Wow wow wow,
So my conclusion is correct, although some MVPs told me that I can't do it and there is no automatic method to do what I want.

Quote:
Originally Posted by Guessed View Post
I was having trouble with the Range object insert before and insertafter...
I know this is not an easy task Mr. Guessed, hence you faced a problem because it's a dynamic field.
Quote:
Originally Posted by Guessed View Post
so I took the unfortunate step of using Selection to get around that issue.
You are awesome, billion thanks Mr. Guessed, May God Protect you everywhere. I appreciate your help and time.
Quote:
Originally Posted by Guessed View Post
but isn't the way I would prefer to do it.
At least, it's Ok now.

Lastly, Mr. Guessed, what I do if I want to apply this VBA code to select range inside the document?

Again, million and billion thanks.
Reply With Quote
  #3  
Old 08-20-2021, 05:06 AM
laith93 laith93 is offline Placing numbers of cross-references of tables and figures inside parenthesis Windows 10 Placing numbers of cross-references of tables and figures inside parenthesis Office 2019
Competent Performer
Placing numbers of cross-references of tables and figures inside parenthesis
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
This appears to work with your sample docs. I would recommend testing it on A COPY of your real documents.
I'm happy to accept criticism and corrections to the Selection object I used.
Hi Mr. Andrew
Excuse me, Dear Andrew, today I applied your code to my entire thesis and I get unexpected results in chapter 4 of my thesis. Firstly, I thought the problem is only with this chapter, but after applying, using different documents, analyzing the problem. Consequently, I have reached the underlying problem.


To clarify, your code works fine for a single occurrence of any cross-references in any chapter, but for second or more occurrences of the same cross-references, it can not deal with them and convert them to a closed parenthesis, see below screenshot
152.jpg

Look when applying your code, what happens
153.jpg

Additionally, when making a right-click on the number of the cross-reference that has multiple occurrences in the document and making update field, the number of cross-reference gone and appear as closed parenthesis like (), as below
154.jpg

For this reason, I get this problem in chapter 4, because I use the same cross-reference many times in my paragraphs.

So, can you fix this problem please with special thanks Mr. Andrew?
The code is amazing and I don't want to leave it.

You can download the attachment file for applying to it.
Attached Files
File Type: docx For thesis.docx (214.6 KB, 10 views)

Last edited by laith93; 08-20-2021 at 01:17 PM.
Reply With Quote
Reply

Tags
vba code, vba editor, word 19



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to update Exhibits and related cross-references numbers automatically? Surendra Yadav Word 4 10-30-2018 03:02 PM
Placing numbers of cross-references of tables and figures inside parenthesis How to convert text that is enclosed inside parenthesis in a word document into numbered footnotes? BobT Word 5 07-27-2018 01:17 PM
How to cross reference figures or tables when option is not given timfoolery Word 4 06-12-2018 05:53 AM
Convert manual cross references in footnotes to other footnotes to automatic cross references ghumdinger Word VBA 7 11-20-2014 11:47 PM
accept only new page numbers and cross references in review mode guitargeorge Word 7 10-15-2013 03:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:28 AM.


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