0
votes

How can we change the position of existing captions of tables in a word document? I have a word document with 100 of tables where caption is added on the top but I need to move it to bottom of the table. My table captions have title as well.

  Sub change_caption_position()
  Application.ScreenUpdating = False
  Dim i As Long
  With ActiveDocument
  For i = .Tables.Count To 1 Step -1
   .Tables(i).Application.Caption = ""
   .Tables(i).Select
  Selection.InsertCaption Label:="Table", TitleAutoText:="", _
  Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
  Next
  End With
  Application.ScreenUpdating = True
  End Sub
1
What's the problem with the code you posted? If it doesn't do what you want, what does it do? - Tim Williams
.Tables(i).Application.Caption = "" does not delete my existing captions so I end up getting two captions on each table. - Abhinav Bajpai
Application.Caption refers to the text displayed in the Title bar of the application window rather than the caption for the table (msdn.microsoft.com/VBA/Word-VBA/articles/…), so setting this will not change the table captions - Jane

1 Answers

0
votes

The Caption property refers to the Application Title Bar rather than a caption within a document. (See https://msdn.microsoft.com/VBA/Word-VBA/articles/application-caption-property-word)

I can't find any way to directly refer to captions within a document. However, captions are stored within a document using fields, so you can access them using the Fields collection. Specifically, a Sequence (SEQ) field is used to generate the number. The rest of the caption is just text.

To find the captions, you can loop through all the fields in the document and identify which are Sequence fields. You may have other types of captions eg for figures, so use the Code property to get the full text of the field and check whether it's for a table. You'll then need to identify the paragraph containing this field and delete it.

Here's a sample:

  Sub Delete_Table_Captions()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument
    For i = .Fields.Count To 1 Step -1
    Dim f As Field
    Set f = .Fields(i)
    If f.Type = wdFieldSequence Then
        Dim s As String
        s = f.Code.Text
        If InStr(1, s, "Table", vbTextCompare) > 0 Then
            f.Code.Paragraphs(1).Range.Text = ""
        End If
    End If

    Next
    End With
    Application.ScreenUpdating = True
  End Sub

Note: This just deletes the original caption, so you'll still need to insert the new one. Your example wasn't copying the existing caption text to the new caption, so if you want to do that, you'll need to store the text from the original captions before deleting them and creating the new ones. You can extract the relevant text from Paragraphs(1).Range.Text