D'Excel vers word

Bonjour,

Quelqu'un aurait-il une idée comment faire avec un code vba pour envoyer un tableau d'une feuille excel à un fichier word.

Merci d'avance

Cordialement

Bonsoir

A tester

' https://docs.microsoft.com/fr-fr/office/vba/excel/concepts/working-with-other-applications/exporting-a-range-to-a-table-in-a-word-document
Sub Export_Table_Data_Word()

    'Name of the existing Word document
    Const stWordDocument As String = "Table Report.docx"

    'Word objects.
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdCell As Word.Cell

    'Excel objects
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet

    'Count used in a FOR loop to fill the Word table.
    Dim lnCountItems As Long

    'Variant to hold the data to be exported.
    Dim vaData As Variant

    'Initialize the Excel objects
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Sheet1")
    vaData = wsSheet.Range("A1:A10").Value

    'Instantiate Word and open the "Table Reports" document.
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)

    lnCountItems = 1

    'Place the data from the variant into the table in the Word doc.
    For Each wdCell In wdDoc.Tables(1).Columns(1).Cells
        wdCell.Range.Text = vaData(lnCountItems, 1)
        lnCountItems = lnCountItems + 1
    Next wdCell

    'Save and close the Word doc.
    With wdDoc
        .Save
        .Close
    End With

    wdApp.Quit

    'Null out the variables.
    Set wdCell = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing

    MsgBox "The " & stWordDocument & "'s table has successfully " & vbNewLine & _
           "been updated!", vbInformation

End Sub

@+

bonsoir,

Merci pour votre réponse, je l'ai testé, il me dit type défini par l'utilisateur non défini pour :Dim wdApp As Word.Application,
Dim wdDoc As Word.Document,
et Dim wdCell As Word.Cell

cordialement

Bonsoir à tous,

Je crois qu'il faut ajouter la référence Microsoft Word X.0 Object Library (où X est un entier). Elle est accessible depuis le menu de l'éditeur : Outils/Références.

Cdlt,

Bonjour,

J'ai ajouté la référence Microsoft Word X.0 Object Library, j'ai ce message quand j'exécute la macro.

Cordialement

meesage2 ligne

Bonjour,

Honnêtement, j'ai répondu pour vous éviter un blocage avec ce petit détail de référence mais je ne saurais pas vous répondre correctement. Je pense que Bruno saura bien mieux vous répondre !

Mais à première vue, je dirais qu'il s'agit d'un problème inexistence d'un objet, une table de document Word. On dirait que ce code suggère qu'elle soit déjà créée. Vous devriez regarder comment créer une table sur word. Il doit exister une méthode Tables.add...

Cdlt,

Merci beaucoup quand même

bonne journée 🙂

Bonjour le fil, Didier0901

J'avoue que l'exemple donné n'est pas très parlant et pas surper pratique (voici les fichiers)

3table-report.docx (12.35 Ko)

Ceci dit, voici ce que j'utilisais à une époque et qui marche très bien sans être complexe

6lettre-rappel1.zip (360.96 Ko)

Dans ce 2ème exemple on écrit des informations dans Word grâce à des balises <MABALISE>

Ca permet également d'y coller des cellules

A vous de mettre la main à la pate maintenant

@+

Bonjour,

Merci beaucoup pour vos réponses

Cordialement

Rechercher des sujets similaires à "word"