We help IT Professionals succeed at work.
Get Started

word document merge with vb.net

Last Modified: 2012-05-11
I had this working, or I thought I did.  I create a document (WdWordTable) with a table to be merged into a word  template document (WdTemplate).  The documents are not merging.

when the document gets to the .Printout part, I get a copy of the word template, not the merged documents.  Opening wdEmployerLetter shows the same, just a template.
Dim WdTemplate As String = "C:\SW_45_Letter.dotx"
Dim WdWordTable As String = "C:\WordTable.docx"
Dim wdEmployerLetter As String = "C:\EmpLetter.docx"

Dim wdApp As New Word.Application()
Dim wdDoc As Word.Document
wdDoc = wdApp.Documents.Add
Dim wdTable As Word.Table = wdDoc.Tables.Add(wdApp.ActiveDocument.Range(0, 0), 1, 8)

wdTable.Rows(1).Cells(1).Range.Text = "EmployerName"
wdTable.Rows(1).Cells(2).Range.Text = "EmployerAddressLine1"
wdTable.Rows(1).Cells(3).Range.Text = "EmployerAddressLine2"
wdTable.Rows(1).Cells(4).Range.Text = "EmployerAddressLine3"
wdTable.Rows(1).Cells(5).Range.Text = "EmployerCity"
wdTable.Rows(1).Cells(6).Range.Text = "EmployerStatePostalCode"
wdTable.Rows(1).Cells(7).Range.Text = "EmployerZipCode"
wdTable.Rows(1).Cells(8).Range.Text = "SharedWorkPlanEndDate"

        For Each wdRow As DataRow In wdDS.Rows
            Dim wRow As Word.Row = wdTable.Rows.Add
            wRow.Cells(1).Range.Text = wdRow.Item("EmployerName")
            wRow.Cells(2).Range.Text = wdRow.Item("EmployerAddressLine1")
            wRow.Cells(3).Range.Text = wdRow.Item("EmployerAddressLine2")
            wRow.Cells(4).Range.Text = wdRow.Item("EmployerAddressLine3")
            wRow.Cells(5).Range.Text = wdRow.Item("EmployerCity")
            wRow.Cells(6).Range.Text = wdRow.Item("EmployerStatePostalCode")
            wRow.Cells(7).Range.Text = wdRow.Item("EmployerZipCode")
            wRow.Cells(8).Range.Text = wdRow.Item("SharedWorkPlanEndDate")

Dim wdMailMergeDoc As Word.Document = wdApp.Documents.Open(WdTemplate)
            wdMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
            wdDoc = Nothing
 wdMailMergeDoc.PrintOut(True, True)

            wdApp = Nothing

Open in new window

Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE