|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
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:
Quote:
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. |
#4
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Thanks, Thanks Thanks Mr. Guessed
I can't thank you enough Thanks for your time and efforts Quote:
Auto list of tables or figures that I talked about: 1.png 2.png Thanks again Mr. Guessed Take your rest Best Regards, laith93 |
#6
|
||||
|
||||
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 |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
See attached.
If you can't work this out, just stick with using the macro. Either way works.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
Quote:
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. Last edited by laith93; 08-20-2021 at 01:17 PM. |
#11
|
||||
|
||||
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 |
#12
|
|||
|
|||
Quote:
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 |
#13
|
||||
|
||||
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 |
#14
|
|||
|
|||
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. |
#15
|
|||
|
|||
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 |
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 |
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 |