Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-23-2021, 03:39 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 Placing numbers of cross-references of tables and figures inside parenthesis

Hi,
Before explaining my problem, I am an academic researcher and I always work with scientific papers or thesis.
Previously, I posted in the Microsoft community, but I didn't get my need.
I recently joined this forum and browsed it for a week and I concluded that everything is possible with VBA, but it requires experience, so I will depend on Mr. macropod, Guessed, gmayor, and Greg Maxey as experts and most posts are replied by them, hence forgive me and you should help me as you can.

My problem (I think it's a worldwide problem) is with table and figure cross-references when inserted in a sentence,
the minimum option available for tables and figures is "Only label and number" for example, "as shown in Table 3.1", see attached image.
I googled for a time on how to insert the only numbers for cross-references of tables and figures, but without benefits, and this is impossible. So we will work with "Only label and number" as minimum option.

So my need is a VBA code to put the number of cross-references inside the parenthesis () and convert the label (Table, Figure) to lowercase because it is not suitable when not come at the start of a sentence.

I can do my need manually, but firstly, it takes a lot of time, secondly, suppose if I delete a certain table or figure, then updating cross-references, it will restore the original format, so the VBA code will solve this issue.

From the attached files, you can download exercise files to apply on it ("For scientific paper" just contain cross-reference number, ex: as shown in Table 3, while "For thesis" the cross-references number contain chapter number additionally, ex: as shown in Table 1.3).

The following image describes what Word can do by default and what I want:
Cross-ref.png

Thank you
Attached Files
File Type: docx For scientific paper.docx (211.0 KB, 7 views)
File Type: docx For thesis.docx (218.4 KB, 7 views)
Reply With Quote
  #2  
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: 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

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
  #3  
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
  #4  
Old 07-24-2021, 06:17 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: 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

To do the replacements only on a selected section of the document, the following minor changes would be needed
Code:
Sub RestrictXRefsSelection()
  Dim aFld As Field, aRngXRef As Range, sWord As String, aRngAnchor As Range, sBkmk As String
  Dim arrCode() As String, aRng As Range
  Set aRng = Selection.Range
  For Each aFld In aRng.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
  aRng.Select
End Sub
You can avoid the need for the macro if you manually bookmark the figure number with a unique name and then create a cross-reference pointing at that bookmark.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 07-24-2021, 11:44 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

Thanks, Thanks Thanks Mr. Guessed
I can't thank you enough
Thanks for your time and efforts

Quote:
Originally Posted by Guessed View Post
You can avoid the need for the macro if you manually bookmark the figure number with a unique name and then create a cross-reference pointing at that bookmark.
I totally agree with you that bookmark is better, but my bookmarks are not static because deleting or inserting a new table or figures will cause changes, also there is no option for inserting a list of tables or figures at the final, which is available with default tables and figures captions. Additionally, if I create a list of tables or figures for bookmarks manually, I will face a problem in the case of deleting or inserting new pages, which also cause changes in the pages of the corresponding bookmarks.


Auto list of tables or figures that I talked about:
1.png
2.png

Thanks again Mr. Guessed
Take your rest

Best Regards,
laith93
Reply With Quote
  #6  
Old 07-25-2021, 10:56 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: 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

I'm saying you select the numbers in the captions themselves and name the bookmarks independent of the actual number that shows eg
2-1 bookmarked as tblTaxEColi
4-1 bookmarked as tblDistUTIbyAge
4-2 bookmarked as tblBactFungGrowth

Then your xRefs can be created as
table ({Ref tblTaxEColi \h})
table ({Ref tblDistUTIbyAge \h})
table ({Ref tblBactFungGrowth \h})
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 07-26-2021, 12:24 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

I'm sorry Mr. Guessed

I can't understand the basis of this method and I tried but without benefit.
What about the list of tables or figures at the end? Is updateable or not??
Can you send me your file, please?

Thanks
Reply With Quote
  #8  
Old 07-26-2021, 01:14 AM
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: 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

See attached.

If you can't work this out, just stick with using the macro. Either way works.
Attached Files
File Type: docx For scientific paper.docx (22.1 KB, 6 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 07-26-2021, 01:33 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

It's an amazing and wonderful way Mr. Guessed

I have just understood the basis of this method.
Thank you so much

Best Regards,
laith93
Reply With Quote
  #10  
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, 8 views)

Last edited by laith93; 08-20-2021 at 01:17 PM.
Reply With Quote
  #11  
Old 08-20-2021, 04:09 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: 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

Yep, that was a pretty big oversight on my behalf. Try this version - again, on a copy of your good document.
Code:
Sub RestrictXRefsSelection()
  Dim aFld As Field, aRngXRef As Range, sWord As String, aRngAnchor As Range, sBkmk As String
  Dim arrCode() As String, aRng As Range, aRngBk As Range
  Set aRng = Selection.Range
  For Each aFld In aRng.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
          If InStr(LCase(aRngAnchor.Text), sWord) > 0 Then
            aRngAnchor.MoveStart Unit:=wdCharacter, Count:=Len(sWord) + 1
            ActiveDocument.Bookmarks.Add Name:=sBkmk, Range:=aRngAnchor
          End If
          aFld.Update
          aFld.Select
          If Trim(LCase(aRngXRef.Words.First.Previous.Text)) <> sWord Then
            Selection.Range.InsertBefore sWord & " ("
            Selection.Range.InsertAfter ")"
          End If
        End If
      End If
    End If
  Next aFld
  aRng.Select
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 08-20-2021, 11:11 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
Default

Quote:
Originally Posted by Guessed View Post
Yep, that was a pretty big oversight on my behalf. Try this version - again, on a copy of your good document.
Excuse me, please be patient with me, and I know I tired you Mr. Andrew

Mr. Andrew, after applying this version of your code, no changes occur even with the first occurrence of certain cross-reference, as shown below
155.jpg
Reply With Quote
  #13  
Old 08-20-2021, 11:43 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: 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

That is what I thought too - until I remembered you wanted me to change the code to only do SELECTED text. Did you select the text you wanted the macro to run on?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #14  
Old 08-21-2021, 12:01 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
Did you select the text you wanted the macro to run on?
I'm so so so sorry Mr. Andrew
I did not pay attention to this, because previously I ran your code for the entire document, for this I applied your code for my entire document without any text selection, so excuse me for this mistake.
However, your edition is amazing and works wonderfully after text selection, as presented below,
156.jpg

Mr. Andrew I can't thank you enough
Thank you so much my bro
You are awesome and brilliant.
I appreciate your help and time.
Reply With Quote
  #15  
Old 08-21-2021, 12:15 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

Mr. Andrew, I edited your code to work for the entire document according to your code
Thank so much again

Code:
Sub RestrictXRefsForEntireDoc()

'Putting cross references number inside parenthesis and convert Figure and Table to lowercase
'Developed by Mr. Andrew - msofficeforums.com

  Dim aFld As Field, aRngXRef As range, sWord As String, aRngAnchor As range, sBkmk As String
  Dim arrCode() As String, aRng As range, aRngBk 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
           If InStr(LCase(aRngAnchor.Text), sWord) > 0 Then
          aRngAnchor.MoveStart Unit:=wdCharacter, Count:=Len(sWord) + 1
          ActiveDocument.Bookmarks.Add Name:=sBkmk, range:=aRngAnchor
          End If
          aFld.Update
          aFld.Select
           If Trim(LCase(aRngXRef.Words.First.Previous.Text)) <> sWord Then
          Selection.range.InsertBefore sWord & " ("
          Selection.range.InsertAfter ")"
        End If
      End If
    End If
    End If
  Next aFld
End Sub
Reply With Quote
Reply

Tags
vba code, vba editor, word 19

Thread Tools
Display Modes


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 07:41 AM.


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