Avatar of bgernon
bgernonFlag for United States of America

asked on 

word document merge with vb.net

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

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Nasir Razzaq
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of bgernon
bgernon
Flag of United States of America image

ASKER

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?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

When you set the destination to NewDocument, would it not create a new document? But you are expecting wdMailMergeDoc to be THAT new document!
Avatar of bgernon
bgernon
Flag of United States of America image

ASKER

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?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of bgernon
bgernon
Flag of United States of America image

ASKER

I see what you mean now.  You're right.  When I make app.visible = True, the document opens.
Avatar of bgernon
bgernon
Flag of United States of America image

ASKER

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Glad you help :-)
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo