VB.NET 2005 WORD Mail Merge using Bookmarks
-
14 Temmuz 2007 Cumartesi 10:48
Hi Everybody out there!
I'm learning VB.net on my own and struggling!
I'm trying to develop a Mail Merge using Bookmarks in a word template (1 page doc). It works ok for the 1st record from a datatable (source of data for bookmarks), and then just piles the other records into the same bookmarks. After the 1st insertion I need the code to goto the end of the doc, insert a page break and append a fresh copy of the template for the next insertion. The mail merge could have many 1 page letters in it.
strTemplateDir = Trim(txtDocument.Text)
wrdApp = CreateObject("Word.Application")
wrdApp.documents.add(strTemplateDir) 'open new word doc based on template
'make it the active doc
'template already has 6 bookmarks created.
'these are:-
'Name
'Add1, Add2, Add3, Add4, Add5
'PostCode
iRecords = CInt(dtNurseries1.Rows.Count)
wrdDoc = wrdApp.activedocument
wrdApp.Visible = True
For i = 0 To iRecords - 1 'remember datatable starts row 0
sName = dtNurseries1.Rows(i).Item("Name")
sAdd1 = dtNurseries1.Rows(i).Item("Add1")
sAdd2 = dtNurseries1.Rows(i).Item("Add2")
sAdd3 = dtNurseries1.Rows(i).Item("Add3")
sPostCode = dtNurseries1.Rows(i).Item("PostCode")
'got the data to insert into Bookmarks
wrdDoc.Bookmarks.Item("Name").Range.Text = sName
wrdDoc.Bookmarks.Item("Add1").Range.Text = sAdd1
wrdDoc.Bookmarks.Item("Add2").Range.Text = sAdd2
wrdDoc.Bookmarks.Item("Add3").Range.Text = sAdd3
wrdDoc.Bookmarks.Item("PostCode").Range.Text = sPostCode
i = i + 1
'need to ensure merge to a fresh page of the template now. ??How??
'wrdDoc.Selection.InsertBreak(Type:=wdPageBreak)
'wrdDoc.document.selection.insertPageBreak()
'wrdDoc.Bookmarks.selection.insertpagebreak()
'wrdDoc.Range.MoveEnd()
'GOTO End of Doc
wrdDoc.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast)
wrdDoc.GoTo(Word.WdBreakType.wdPageBreak)
'wrdDoc.Select(Word.WdTemplateType.wdAttachedTemplate = WdTemplateType.wdAttachedTemplate)
Next
dNow = Now()
'strMergedName = CStr(My.Settings.Path & cboArea.Text & "Merged" & dNow.ToString("d") & ".doc")
strMergedName = CStr(My.Settings.Path & cboArea.Text & "Merged.doc")
wrdApp.ActiveDocument.SaveAs(strMergedName)
wrdDoc = Nothing
wrdApp = Nothing
End Sub
Any ideas would be much appreciated. thanks.
Tüm Yanıtlar
-
17 Temmuz 2007 Salı 09:11
Hi
I have the same problem, I have 1 template from which I want to create multiple pages in the same document
Has anyone found a solution yet?
Thanks
-
19 Temmuz 2007 Perşembe 09:49
Waldek and Parus2 ,
There is a KB article on mail merge in VB.NET. I tested the code and it works well on my side. Please try to run the application in the article and I hope that can help you with the problem:
How to automate Word to perform a mail merge from Visual Basic .NET
http://support.microsoft.com/kb/301656/
Word exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. For example, there is an Application object, a Document object, and a Paragraph object, each of which contain the functionality of those components in Word. To access the object model from Visual Basic .NET, you can set a project reference to the type library.
This article demonstrates how to set the proper project reference to the Word type library for Visual Basic .NET and provides sample code to automate Word.
-
03 Nisan 2012 Salı 07:45
Hi,
Mail merge is usually defined with merge fields, not bookmarks. MS Word as many other Word libraries use merge fields to identify placeholders for data.
If you are working with VB.NET DOCX file format, you can easily do VB.NET Word mail merge so that each record gets imported to its own page - with this C# / VB.NET Word component.
Here is a sample VB.NET code:
' Use the component in free mode. ComponentInfo.SetLicense("FREE-LIMITED-KEY") ' Create a DataTable with letter recipients. Dim dataTable = New DataTable() dataTable.Columns.Add("FullName", GetType(String)) dataTable.Columns.Add("Location", GetType(String)) dataTable.Rows.Add("John Doe", "New York City") dataTable.Rows.Add("Fred Nurk", "Chicago") dataTable.Rows.Add("Hans Meier", "Los Angeles") ' Create a template document for demonstration purpose. Dim document = New DocumentModel() document.Sections.Add( New Section(document, New Paragraph(document, New Run(document, "Recipient:"), New SpecialCharacter(document, SpecialCharacterType.Tab), New Field(document, FieldType.MergeField, "FullName"), New SpecialCharacter(document, SpecialCharacterType.LineBreak), New Run(document, "Location:"), New SpecialCharacter(document, SpecialCharacterType.Tab), New Field(document, FieldType.MergeField, "Location")))) ' If you have already existing template document, load it like this: ' Dim document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault) ' Execute a mail merge. Letter for every recipient will be on a new page. document.MailMerge.Execute(dataTable) ' Save the document to a file. document.Save("Document.docx", SaveOptions.DocxDefault)