Using VBA to mailmerge non-linear table in MS WORD with an excel table

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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật