![]()  | 
	
| 
		 
			 
			#1  
			 
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 
			
			Hi, 
		
		
		
		
		
		
		
		
	
	I have defined two styles (CapitonTable and CaptionFigure) with different formats, I want a VBA code that finds and replaces a default caption style for tables to CapitonTable style, and in the same way for figures to CaptionFigure. Thanks  | 
| 
		 
			 
			#2  
			 
			
			
			
			
		 
		
	 | 
||||
		
		
  | 
||||
| 
		
	
		
		
			
			 
			
			You can do this with find/replace and can also record it if you want it as a macro. Basic process is: 
		
		
		
		
1. Show all field codes (by pressing Alt-F9) 2. Do a find/replace and search for "seq table" and replace with the style 3. Do a find/replace and search for "seq figure" and replace with the style 4. Reveal all field values (by pressing Alt-F9) 
				__________________ 
		
		
		
		
		
	
	Andrew Lockton Chrysalis Design, Melbourne Australia  | 
| 
		 
			 
			#3  
			 
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 Quote: 
	
 You are the Best Best Regards Here is the macro code for one who concern: 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 = Not ActiveWindow.View.ShowFieldCodes
    
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    ' Replace "CapTbl" with your own alternative caption style for tables
    Selection.Find.Replacement.Style = ActiveDocument.Styles("CapTbl")
    
    With Selection.Find
        .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
    End With
    
    
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    ' Replace "CapFig" with your own alternative caption style for figures
    Selection.Find.Replacement.Style = ActiveDocument.Styles("CapFig")
    
    With Selection.Find
        .Text = "seq figure"
        .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
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    ' Hiding all field codes (or by pressing Alt-F9):
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub
 | 
| 
		 
			 
			#4  
			 
			
			
			
			
		 
		
	 | 
||||
		
		
  | 
||||
| 
		
	
		
		
			
			 
			
			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  | 
| 
		 
			 
			#5  
			 
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 Quote: 
	
 Thanks for your warning and note We keep learning Thanks again Mr. Andrew  | 
| 
		 
			 
			#6  
			 
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 Quote: 
	
 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.  | 
| 
		 
			 
			#7  
			 
			
			
			
			
		 
		
	 | 
||||
		
		
  | 
||||
| 
		
	
		
		
			
			 
			
			The issue is that your figure captions are not in the main body of the document. Your example has the graphic floating and the caption is sitting in a text frame. To solve that, you need the macro to find the storyranges which might contain an entry. 
		
		
		
		
Code: 
	Sub ReplaceCaptionStyle()
' Developed by Andrew Lockton (Guessed)
' Edited by laith93
' www.msofficeforums.com
  Dim aRng As Range
' Showing all field codes (or by pressing Alt-F9):
  ActiveWindow.View.ShowFieldCodes = True
  
  For Each aRng In ActiveDocument.StoryRanges
    With aRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      ' Replace "CapTbl" with your own alternative caption style for tables
      .Replacement.Style = ActiveDocument.Styles("CapTbl")
      .Text = "seq table"
      .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
  Next aRng
    
  ' Hiding all field codes (or by pressing Alt-F9):
  ActiveWindow.View.ShowFieldCodes = False
End Sub
				__________________ 
		
		
		
		
		
	
	Andrew Lockton Chrysalis Design, Melbourne Australia  | 
| 
		 
			 
			#8  
			 
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 Quote: 
	
 You are completely right I always use the wrap option for figures to "Top and Bottom", and the caption inserted to a text box automatically. Thanks Mr. Andrew for your assistance and guidance Best Regards  | 
 
 | 
	
	
| Tags | 
| word 2019, word vba, word vba macro | 
| 
		 | 
			 
			Similar Threads
		 | 
	||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
		
		  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 | 
		
		  Trying to customize caption text for figure table
	 | 
	canadianjameson | Word | 6 | 04-02-2018 07:44 PM | 
		
		  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 | 
		
		  Changing style of caption heading
	 | 
	NicholaB | Word | 6 | 12-20-2012 04:28 PM |