troubleshooting Question

word document merge with vb.net

Avatar of bgernon
bgernonFlag for United States of America asked on
Visual Basic.NET.NET Programming
8 Comments1 Solution661 ViewsLast Modified:
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")
        Next

wdDoc.SaveAs(WdWordTable)
            wdDoc.Close()
Dim wdMailMergeDoc As Word.Document = wdApp.Documents.Open(WdTemplate)
            wdMailMergeDoc.MailMerge.OpenDataSource(Name:=WdWordTable)
            wdMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
            wdMailMergeDoc.MailMerge.Execute()
wdMailMergeDoc.SaveAs(wdEmployerLetter)
            wdDoc = Nothing
 wdMailMergeDoc.PrintOut(True, True)

            wdMailMergeDoc.Close()
            wdApp.Quit()
            wdApp = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros