I want to mailmerge a word document containing unlinear table from Microsoft word with an excel table. The word and excel documents contains table with 3 parameters to mailmerge named “Exigence”, “NC” and “Commentaire” Each entries in the excel table must give for result a new table in the same word document with value from excel table and leap line between each table so the code must allow user to positioning the entries in the word table because the “NC” and “Exigences” will mailmerge in the same cell in word document. The code must also let the user choose the excel file location via a dialog box. I also precise that the parameters “Commentaire” has a long text which can print in one page so be aware of that when you will chose the type for this variable.
The problem with this code is that the result is a word doc with the duplicate table without the values from the excel table for each values, Someone to help me.
CODE I PRODUCE :
Sub MailMergeFromExcel()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
Dim dlgOpen As FileDialog
Dim xlFilePath As String
Dim i As Integer
Dim tbl As Word.Table
Dim rng As Word.Range
Dim newTable As Word.Table
Dim cell As Word.Cell
Dim cellText As String
' Initialize Word Application
Set wdApp = Application
Set wdDoc = wdApp.ActiveDocument
' Initialize Excel Application
Set xlApp = New Excel.Application
' Open file dialog to select Excel file
Set dlgOpen = xlApp.FileDialog(msoFileDialogOpen)
dlgOpen.Title = "Select the Excel File"
dlgOpen.Filters.Add "Excel Files", "*.xls; *.xlsx", 1
If dlgOpen.Show <> -1 Then Exit Sub ' User canceled
xlFilePath = dlgOpen.SelectedItems(1)
' Open the selected Excel file
Set xlWb = xlApp.Workbooks.Open(xlFilePath)
Set xlWs = xlWb.Sheets(1)
' Loop through each row in the Excel table
For i = 2 To xlWs.UsedRange.Rows.Count ' Assuming first row is headers
' Find the template table
Set tbl = wdDoc.Tables(1) ' Assumes the template table is the first table in the document
' Copy the template table
tbl.Range.Copy
' Insert a new table based on the template
Set rng = wdDoc.Range
rng.Collapse wdCollapseEnd
rng.InsertParagraphAfter
rng.Collapse wdCollapseEnd
rng.Paste
Set newTable = wdDoc.Tables(wdDoc.Tables.Count)
' Replace placeholders with Excel data
For Each cell In newTable.Range.Cells
cellText = cell.Range.Text
cellText = Replace(cellText, Chr(13) & Chr(7), "") ' Remove end of cell marker
Select Case cellText
Case "{Exigence}"
cell.Range.Text = xlWs.Cells(i, 1).Value ' Assuming "Exigence" is in column A
Case "{NC}"
cell.Range.Text = xlWs.Cells(i, 2).Value ' Assuming "NC" is in column B
Case "{Commentaire}"
cell.Range.Text = xlWs.Cells(i, 3).Value ' Assuming "Commentaire" is in column C
Case Else
Debug.Print "Placeholder not found: " & cellText
End Select
Next cell
' Add a line break after the table
newTable.Range.InsertParagraphAfter
newTable.Range.Paragraphs.Last.Range.InsertParagraphAfter
Next i
' Cleanup
xlWb.Close False
xlApp.Quit
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub
Excel table sample
enter image description here
Word table Example
enter image description here
Guy-Ghislain APPIAH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.