Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2021, 07:41 PM
Guessed's Avatar
Guessed Guessed is offline Replace table and figure caption style to another style Word VBA Windows 10 Replace table and figure caption style to another style Word VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,187
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

Here is your code tidied up a bit. Note that the toggling of field codes makes an assumption that codes were not showing before the macro ran. That is risky so I changed those to explicitly set the value instead of toggling true/false.


Code:
Sub ReplaceCaptionStyle()
' Developed by Andrew Lockton (Guessed)
' Edited by laith93
' www.msofficeforums.com

' Showing all field codes (or by pressing Alt-F9):
  ActiveWindow.View.ShowFieldCodes = True
  
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    ' Replace "CapTbl" with your own alternative caption style for tables
    .Replacement.Style = ActiveDocument.Styles("CapTbl")
    .Text = "seq table"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    
    ' Replace "CapFig" with your own alternative caption style for figures
    .Replacement.ClearFormatting
    .Replacement.Style = ActiveDocument.Styles("CapFig")
    .Text = "seq figure"
    .Execute Replace:=wdReplaceAll
  End With
    
  ' Hiding all field codes (or by pressing Alt-F9):
  ActiveWindow.View.ShowFieldCodes = False
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #2  
Old 08-26-2021, 07:53 PM
laith93 laith93 is offline Replace table and figure caption style to another style Word VBA Windows 10 Replace table and figure caption style to another style Word VBA Office 2019
Competent Performer
Replace table and figure caption style to another style Word VBA
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Here is your code tidied up a bit. Note that the toggling of field codes makes an assumption that codes were not showing before the macro ran. That is risky so I changed those to explicitly set the value instead of toggling true/false.
It's an amazing idea and new info for me in VBA
Thanks for your warning and note
We keep learning
Thanks again Mr. Andrew
Reply With Quote
  #3  
Old 08-27-2021, 01:29 PM
laith93 laith93 is offline Replace table and figure caption style to another style Word VBA Windows 10 Replace table and figure caption style to another style Word VBA Office 2019
Competent Performer
Replace table and figure caption style to another style Word VBA
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Here is your code tidied up a bit. Note that the toggling of field codes makes an assumption that codes were not showing before the macro ran. That is risky so I changed those to explicitly set the value instead of toggling true/false.
Excuse me, Mr. Andrew, I think we have a mistake in our code,
when applying this code directly, only styles of the table caption changed to "CapTbl" style, while styles of the figure caption remain unchanged.

I tried to make a separate macro for each one, but only for tables work fine, while for figure ones do not work at all, so you need to use the F&R box for figures, hence is there any mistake for the figure part in our code?

However, using F&R box works fine for both tables and figures.
Also, you can download "Test" in the attachment to apply on it and explore the problem.
Attached Files
File Type: docm Test.docm (33.9 KB, 8 views)
Reply With Quote
Reply

Tags
word 2019, word vba, word vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace table and figure caption style to another style Word VBA Word 2007: Unable to change character style, when using a linked Char/Para style format Last Chance Word 3 06-09-2021 12:52 PM
Replace style in all tables in a word document. edgar Word VBA 5 04-02-2019 04:25 PM
Replace table and figure caption style to another style Word VBA Trying to customize caption text for figure table canadianjameson Word 6 04-02-2018 07:44 PM
Replace table and figure caption style to another style Word VBA Use multiple style sets in the same Word document (depending on which section the style is in) Ricyteach Word VBA 6 03-09-2015 07:11 PM
Replace table and figure caption style to another style Word VBA Changing style of caption heading NicholaB Word 6 12-20-2012 04:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:58 PM.


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