Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 10-22-2021, 01:41 AM
Guessed's Avatar
Guessed Guessed is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

There is a complexity in using ranges as adding the brackets can often end up inside the field itself. For this reason you need to use the Selection object which is kind of a pain. Try this code version


Code:
Sub AppendixParens()
'   Charles Kenyon and Andrew Lockton 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, iCount As Integer, aRng As Range
    On Error Resume Next
      For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
          If oFld.Type = wdFieldSequence Then
            If oFld.Code Like "*Table*" Then
              Set aRng = oFld.Result
              oFld.Select       'need to use selection object to make insertbefore go outside of field
              Selection.Range.InsertBefore "("
              Selection.Range.InsertAfter ")"
              iCount = iCount + 1
            End If
          End If
        Next oFld
      Next
    Set oFld = Nothing
    Set oStory = Nothing
    On Error GoTo 0
    MsgBox iCount & " instances bracketed.", vbOKOnly, "Macro complete"
End Sub
Note that I think these brackets are going to play badly with cross-references to the captions.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #17  
Old 10-22-2021, 01:56 AM
Guessed's Avatar
Guessed Guessed is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

Actually, I think this is a massive improvement and avoids the problem with cross-references. This one changes the number format of the field itself so the brackets are part of the field itself.
Code:
Sub AppendixParensIntegrated()
'   Charles Kenyon and Andrew Lockton 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, iCount As Integer, aRng As Range
    On Error Resume Next
      For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
          If oFld.Type = wdFieldSequence Then
            If oFld.Code Like "*Table*" Then
              oFld.Code.Text = "SEQ Table \# ""(#)"" "
              iCount = iCount + 1
            End If
          End If
        Next oFld
      Next
      ActiveDocument.Fields.Update
    Set oFld = Nothing
    Set oStory = Nothing
    On Error GoTo 0
    MsgBox iCount & " instances bracketed.", vbOKOnly, "Macro complete"
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #18  
Old 10-22-2021, 04:42 AM
Charles Kenyon Charles Kenyon is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,138
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Andrew, I ran your macro on the sample file provided and it did nothing because of the term "Table." When I changed it to "Appendix" it caught the in-line instance but not the one in the Textbox.


Code:
Sub AppendixParensIntegrated()
'   Charles Kenyon and Andrew Lockton using code from Graham Mayor
'   October 21, 2021
'   https://www.msofficeforums.com/word/47835-putting-parentheses-around-sequence-number-caption-label.html
'   Instead of adding () characters, change the field's display format to include them
    Dim oStory As range, oFld As Field, iCount As Integer, aRng As range
    On Error Resume Next
      For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
          If oFld.Type = wdFieldSequence Then
            If oFld.Code Like "*Appendix*" Then
              oFld.Code.Text = "SEQ Appendix \# ""(#)"" "
              iCount = iCount + 1
            End If
          End If
        Next oFld
      Next oStory
      ActiveDocument.Fields.Update
    Set oFld = Nothing
    Set oStory = Nothing
    On Error GoTo 0
    MsgBox iCount & " instances bracketed.", vbOKOnly, "Macro complete"
End Sub
Reply With Quote
  #19  
Old 10-22-2021, 04:55 AM
Charles Kenyon Charles Kenyon is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,138
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

With this document, in the Immediate panel the following results in 0:
Code:
? ActiveDocument.StoryRanges(wdTextFrameStory).Fields.Count
  0
There is a field in the textbox.
With the main story, the result is 1.
Reply With Quote
  #20  
Old 10-23-2021, 03:04 AM
Guessed's Avatar
Guessed Guessed is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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 thought with my earlier testing the code worked on both instances (which at that time were Sequence Table fields) but now I've retested it I can see it only caught the inline field. Now it appears that the first field is in a Shape object so the code would change to ...
Code:
Sub AppendixParensIntegrated2()
'   Charles Kenyon and Andrew Lockton 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, iCount As Integer, aRng As Range, aShape As Shape
    On Error Resume Next
      For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
         If oFld.Type = wdFieldSequence Then
            If oFld.Code Like "*Appendix*" Then
              oFld.Code.Text = "SEQ Appendix \# ""(#)"" "
              iCount = iCount + 1
            End If
          End If
        Next oFld
      Next
      For Each aShape In ActiveDocument.Shapes
        If aShape.TextFrame.HasText Then
          For Each oFld In aShape.TextFrame.TextRange.Fields
            If oFld.Type = wdFieldSequence Then
              If oFld.Code Like "*Appendix*" Then
                oFld.Code.Text = "SEQ Appendix \# ""(#)"" "
                iCount = iCount + 1
              End If
            End If
            oFld.Update
          Next oFld
        End If
      Next aShape
      
      ActiveDocument.Fields.Update
    Set oFld = Nothing
    Set oStory = Nothing
    On Error GoTo 0
    MsgBox iCount & " instances bracketed.", vbOKOnly, "Macro complete"
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #21  
Old 10-23-2021, 11:40 PM
laith93 laith93 is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Competent Performer
Putting parentheses around sequence number of caption label
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by wiganken View Post
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.
Sorry for the late reply, because I being so busy
Ok, good idea, but who can speak with faculty members?!
Our love, respect, and thanks to real-world experts like Andrew, Graham, and Charles
I can't thank them enough
I appreciate their time and efforts
My best wishes to them.
Reply With Quote
  #22  
Old 10-23-2021, 11:43 PM
laith93 laith93 is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Competent Performer
Putting parentheses around sequence number of caption label
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Can you post a sample document, or send it to my web site.
Sorry for the late reply, because I being so busy

You can download the sample file from the attachment, but the last code by Mr. Andrew works amazing and absolutely.
I think you should take your rest, as the problem has been solved.

Thank you
Thank you, I really appreciate your help
Thank you, That’s very kind of you.
Attached Files
File Type: docx Sample.docx (190.1 KB, 4 views)
Reply With Quote
  #23  
Old 10-23-2021, 11:53 PM
laith93 laith93 is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Competent Performer
Putting parentheses around sequence number of caption label
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Charles Kenyon View Post
Andrew, I ran your macro on the sample file provided and it did nothing because of the term "Table." When I changed it to "Appendix" it caught the in-line instance but not the one in the Textbox.
I'm sorry for this mistake, because at first I was trying on any caption, just to obtain the result, so forgive me for this mistake.

Thank you Mr. Charles
Thank you for your time and interest
Best Regards
Reply With Quote
  #24  
Old 10-24-2021, 12:02 AM
laith93 laith93 is offline Putting parentheses around sequence number of caption label Windows 10 Putting parentheses around sequence number of caption label Office 2019
Competent Performer
Putting parentheses around sequence number of caption label
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Smile

Quote:
Originally Posted by Guessed View Post
I thought with my earlier testing the code worked on both instances (which at that time were Sequence Table fields) but now I've retested it I can see it only caught the inline field. Now it appears that the first field is in a Shape object so the code would change to ...

Wow, Well done!
Billion Thanks Mr. Andrew
The code solved my problem and worked in the right way
Mr. Andrew,
Thank you, You’re so helpful
All my love and thanks to you
I can’t thank you enough for helping me
I will never forget what you have done
Words can’t describe how thankful I am
Thank you
Reply With Quote
Reply

Tags
find & replace, find replace;wildcards, word 19



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula for number sequence 14spar15 Excel Programming 4 03-18-2018 07:00 PM
Putting parentheses around sequence number of caption label Assigning a string variable to a userform label caption Larry_1 Excel Programming 3 12-18-2017 06:59 AM
Putting parentheses around sequence number of caption label how to have two formats in the Caption style: Label – number - text? Jamal NUMAN Word 39 03-14-2017 06:32 PM
Putting parentheses around sequence number of caption label 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:12 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