#1
|
|||
|
|||
Putting parentheses around sequence number of caption label
Hi,
I created a custom label for captioning figures and tables, see below 02.png I want to put parentheses () around the sequential number to appear as below (Manually created through editing field code): 03.png I tried to find and replace through showing all field codes (by pressing Alt-F9), and then searching for {Seq Appendix \* Arabic} and replace it to ({Seq Appendix \* Arabic}) My problem is each time, it tells me "No result found" but when removing braces {}, a result appears So how to fix this problem or any other method to put parentheses around sequence number of caption label. |
#2
|
|||
|
|||
Are the parentheses necessary or just a preference? Just wondering if you are making problems for yourself, that's all. Maybe accept the label without parentheses? Just a thought.
Hope you get an answer though. |
#3
|
||||
|
||||
A simple macro should do the trick
Code:
Sub Macro1() Dim oFld As Field For Each oFld In ActiveDocument.Fields If oFld.Type = wdFieldSequence Then If InStr(1, oFld.Code, "Appendix") > 0 Then oFld.Result.InsertAfter ")" oFld.Result.InsertBefore "(" End If End If Next oFld Set oFld = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
Quote:
But what I can do with faculty preferences, they inform me to use this pattern. |
#5
|
|||
|
|||
Thank you so much my bro
It works fine, but it has a problem It does its job if the text is present in the main body of the document, but if the text is present in the text box (I always make a group for the figure and its caption), it ignores it. See below, Appendix 1 caption is present in the text box, while Appendix 2 is present in the main body of the document. 04.png Hence my bro fixes it if you can. Thank you again |
#6
|
|||
|
|||
|
#7
|
|||
|
|||
Quote:
Again the above macros work to find and replace normal text anywhere, but do not look for the text of field codes ex: {Seq Appendix \* Arabic}. Here the sample file you can try to explore what I mean. Last edited by laith93; 10-21-2021 at 09:25 PM. |
#8
|
|||
|
|||
Quote:
Code:
Sub AppendixParens() ' Charles Kenyon using code from Graham Mayor ' October 21, 2021 ' https://www.msofficeforums.com/word/47835-putting-parentheses-around-sequence-number-caption-label.html Dim oStory As range, oFld as Field On Error Resume Next For Each oStory In ActiveDocument.StoryRanges Do For Each oFld In oStory.Fields If oFld.Type = wdFieldSequence Then If InStr(1, oFld.Code, "Appendix") > 0 Then oFld.Result.InsertAfter ")" oFld.Result.InsertBefore "(" End If End If Next oFld Loop Until oStory Is Nothing Next Set oFld = Nothing Set oStory = Nothing On Error GoTo -1 End Sub |
#9
|
|||
|
|||
Thank you again, Mr. Charles
I applied your code on my sample file, as it contains only two caption It takes about 15 mins without any changes until an error message appears "Not responding" 05.png My laptop RAM is almost full, and fan speed was increased, although I have a laptop with (CPU Intel Core i7, RAM 8GB, GPU 2GB). Any recommendation, please? |
#10
|
||||
|
||||
Documents comprise several separate story ranges which make up the whole - much like a stack of transparencies.
The code I posted earlier acts only on the main story range i.e. the body of the document. Where other ranges need to be processed you meed to address those ranges separately. Code:
Sub Macro1() Dim oFld As Field Dim oStory As Range For Each oStory In ActiveDocument.StoryRanges For Each oFld In oStory.Fields If oFld.Type = wdFieldSequence Then If InStr(1, oFld.Code, "Appendix") > 0 Then oFld.Result.InsertAfter ")" oFld.Result.InsertBefore "(" End If End If Next oFld oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange For Each oFld In oStory.Fields If oFld.Type = wdFieldSequence Then If InStr(1, oFld.Code, "Appendix") > 0 Then oFld.Result.InsertAfter ")" oFld.Result.InsertBefore "(" End If End If Next oFldoStory.Fields.Update Wend End If DoEvents Next oStory lbl_Exit: Set oStory = Nothing Set oFld = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#11
|
|||
|
|||
Quote:
I appreciate your time and effort Mr. Graham, this error message appears 06.png |
#12
|
||||
|
||||
Sorry I had just spotted that and was about to correct it
There's a missing line break in that line Change Code:
Next oFldoStory.Fields.Update Code:
Next oFld oStory.Fields.Update
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#13
|
|||
|
|||
Don't worry my bro I'm sorry for annoying and be patient Mr. Graham, the error message was solved, but there are no changes in the document??? |
#14
|
|||
|
|||
I hope Graham and Charles fix it for you but you may need a fall back plan: - If real-world experts like Graham and Charles don't find an answer then consider telling the faculty that their parentheses requirement isn't practical and ask them to remove the requirement.
Just a thought. Best wishes for a good result though. |
#15
|
||||
|
||||
Can you post a sample document, or send it to my web site.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
find & replace, find replace;wildcards, word 19 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formula for number sequence | 14spar15 | Excel Programming | 4 | 03-18-2018 07:00 PM |
Assigning a string variable to a userform label caption | Larry_1 | Excel Programming | 3 | 12-18-2017 06:59 AM |
how to have two formats in the Caption style: Label – number - text? | Jamal NUMAN | Word | 39 | 03-14-2017 06:32 PM |
Custom caption list label lost in a new Word session | New Daddy | Word | 1 | 09-22-2013 09:21 AM |
Custom caption label annoyance | zac | Word | 1 | 08-29-2010 09:56 AM |