word document merge with vb.net

bgernon
bgernon used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try printing directly

wdMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter

Author

Commented:
That works.  But I'd like to save the document also.  It saves, but when I try to open the document, I get the message 'The file EmpLetter.docx cannot be opened because there are problems with the contents.  Any thoughts?
Most Valuable Expert 2012
Top Expert 2014

Commented:
When you set the destination to NewDocument, would it not create a new document? But you are expecting wdMailMergeDoc to be THAT new document!
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Yes, I was expecting 'wdMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument' to create a new document.  Then I thought I could save it with 'wdMailMergeDoc.SaveAs(wdEmployerLetter'.  Wrong?
Most Valuable Expert 2012
Top Expert 2014

Commented:
But the wdMailMergeDoc is the template document not a new document isn't it? If you make Word visible, does it show a third document open?

Author

Commented:
I see what you mean now.  You're right.  When I make app.visible = True, the document opens.

Author

Commented:
I've thought about it and can't see a reason to save the document.  I'm going with 'wdMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter'.

Thank you
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad you help :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial