'How to change the size and the position of the table in the footer?

i need to create a table in the footer of a .docx using excel vba. I have my cell but do you know if there is any way to change the size and the position of it ? Thanks

Edit : i found this : https://docs.microsoft.com/en-us/office/vba/api/word.tables.add

But idk what to put in the DefaultTableBehavior and AutoFitBehavior

Option Explicit

Sub test()

'Déclaration des variables
Dim I As Integer
Dim Fichier As String
Dim MaFeuille As Worksheet
Dim ListeBordures As Variant
Dim word_app As Word.Application, word_fichier As Word.Document, tbl As Word.Table, rngCell As Word.Range

'On récupère le fichier test

    Fichier = ActiveWorkbook.Path & "\" & "test.docx"

'Ouverture de word
    Set word_app = CreateObject("Word.Application")
    With word_app
         .Visible = True
         .WindowState = 1
         Set word_fichier = .Documents.Open(Fichier)
   End With

'Test tableau bas de page
   With word_fichier
        Set tbl = .Tables.Add(.Sections(1).Footers(wdHeaderFooterPrimary).Range, 1, 1)
        ListeBordures = Array(-1, -2, -3, -4)
        With tbl
             .Cell(1, 1).Range.Text = "test"
             For I = LBound(ListeBordures) To UBound(ListeBordures)
                 With .Borders(ListeBordures(I))
                      .LineStyle = wdLineStyleDot
                      .LineWidth = wdLineWidth100pt
                 End With
             Next I
        End With
   End With

   Set tbl = Nothing: Set word_fichier = Nothing: Set word_app = Nothing

End Sub


Solution 1:[1]

After hours of research i found these lines :


Sub test()

'Déclaration des variables
Dim I As Integer
Dim Fichier As String
Dim MaFeuille As Worksheet
Dim ListeBordures As Variant
Dim word_app As Word.Application, word_fichier As Word.Document, tbl As Word.Table, rngCell As Word.Range

'On récupère le fichier test

    Fichier = ActiveWorkbook.Path & "\" & "test.docx"
    

'Ouverture de word
    Set word_app = CreateObject("Word.Application")
    With word_app
         .Visible = True
         .WindowState = 1
         Set word_fichier = .Documents.Open(Fichier)
   End With

'Test tableau bas de page
   With word_fichier
        Set tbl = .Tables.Add(.Sections(1).Footers(wdHeaderFooterPrimary).Range, 1, 1, wdWord8TableBehavior)
        ListeBordures = Array(-1, -2, -3, -4)
        With tbl
             .Cell(1, 1).Range.Text = "test"
             .Rows.HorizontalPosition = InchesToPoints(1)
             .Rows.VerticalPosition = InchesToPoints(-2)
             .Columns(1).SetWidth ColumnWidth:=310.7, RulerStyle:=1
             For I = LBound(ListeBordures) To UBound(ListeBordures)
                 With .Borders(ListeBordures(I))
                      .LineStyle = wdLineStyleSingle
                      .LineWidth = wdLineWidth225pt
                      .Color = RGB(255, 0, 0)
                 End With
             Next I
        End With
   End With
   
 
   Set tbl = Nothing: Set word_fichier = Nothing: Set word_app = Nothing

End Sub

With .Rows.HorizontalPosition = InchesToPoints(1) and .Rows.VerticalPosition = InchesToPoints(-2) for positioning

And .Columns(1).SetWidth ColumnWidth:=310.7, RulerStyle:=1 for sizing

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 julien1h