Add Captions to every table in a Word document using a few lines of VBA | Auto-numbering Word tables
HTML-код
- Опубликовано: 11 сен 2024
- Add Captions to every table in a Word document using a few lines of VBA|| Use VBA to automatically insert captions to every table in Word.
Let’s see how we can save time and effort and add a caption to every table in a document at once, without having to go through each table and use the ribbon every time, by using less than half a dozen lines of VBA.
First we define an integer variable that will help us loop through every table in our document, then we perform the loop by using the integer variable and the Count property.
The next line selects the current table in the loop and then we assign a Table label to our table. We leave the title blank, since this is something that will have to do manually, and then we set the position of the caption which can be above the table as in here, or below the table if we change this string, which is an enumerator, to wdCaptionPositionBelow.
Optionally, you can also change the caption’s style, as I do here, setting its properties, for example the font, font size and font color, and whether the text should be in bold or italics.
With around half a dozen lines of VBA you can add a caption to every table in a Word document in a few seconds!
Thanks for watching!
Here's the code that you can use:
Sub AddCaptions()
Dim i As Integer
i = 1
'loop through the document's tables
For i = 1 To ActiveDocument.Tables.Count
'Insert a Table caption above every document in the table
ActiveDocument.Tables.Item(i).Select
Selection.InsertCaption Label:="Table", _
Title:=". ", _
Position:=wdCaptionPositionAbove
Next i
'Optional
With ActiveDocument.Styles("Caption").Font
.Name = "Times New Roman"
.Size = 10
.Italic = False
.Bold = True
.ColorIndex = wdBlack
End With
End Sub
Promo*SM 😂