Mise en place d'un tableau dans un bas de page word depuis Excel VBA

Bonjour à tous,

Je souhaite créer un tableau dans mon bas de page (Word) depuis excel, à terme cette macro permettra d'automatiser des documents qui vont plusieurs centaines de pages. J'ai commencé à faire quelques recherches et je butte sur certains aspects.

Je n'arrive pas à créer mon tableau, j'ai une erreur "Erreur d'execution 91 : Variable objet ou variable de bloc With non définie"

Voici mon code

Private Sub test()

'Déclaration des variables
    Dim MaFeuille As Worksheet
    Dim word_app As Word.Application
    Dim word_fichier As Word.Document
    Dim tbl As Word.Table
    Dim rngCell As Word.Range
    Dim fichier As String

'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
        End With

'Test tableau bas de page
    With word_fichier
            Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 2)
    End With

    With tbl
        .Cell(1, 1).Range.Text = "test"
    End With

End Sub

Merci d'avance :)

Edit

Bonjour,

Il faut ouvrir votre fichier...

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, 2, 2)
        ListeBordures = Array(-1, -2, -3, -4, 5, -6)
        With tbl
             .Cell(1, 1).Range.Text = "test"
             For I = LBound(ListeBordures) To UBound(ListeBordures)
                 With .Borders(ListeBordures(I))
                      .LineStyle = Options.DefaultBorderLineStyle
                      .LineWidth = Options.DefaultBorderLineWidth
                 End With
             Next I
        End With
   End With

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

End Sub

Merci bcp, effectivement j'avais oublié d'ouvrir le fichier

Contre je ne connais pas du tout les Arrays je vais me pencher là dessus pour comprendre comment ça fonctionne.

Merci bcp :)

J'ai bien compris comment ce bout de code fonctionne, encore merci.

Mais si je veux réduire la taille de la cellule ou encore bouger son emplacement, est ce que c'est possible ? Merci davance.

Il vous faut éventuellement préciser comment doit s'afficher le texte dans la cellule avec WordWrap mais qui doit être à True par défaut.

             With .Cell(1, 1)
                  .Range.Text = "test"
                  .WordWrap = True
             End With

Merci :), ma question était plutôt comment je passe de ça :

word1

à ça (j'ai fait un dessin sur paint)

word2

J'ai trouvé pour ceux que ça intéresse :

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, 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
Rechercher des sujets similaires à "mise place tableau bas page word vba"