Free Support Forum - aspose.com

Mail Merge In word .net

I am kinda lost here.I downloaded the trial version and I am trying to do mailmerge as I want it to so i shall make up my mind to buy this product.however I just trying create a small app using your dinnerinvitationdemo. Below is the code.I am getting a syntax error in

ExecuteDataTable

Imports Microsoft.VisualBasic

Imports System.Data

Imports Aspose.Words

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Btnrun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btnrun.Click

Dim docpath As String = "C:\CWEB\"

Dim doc As Document = New Document(System.IO.Path.Combine(docpath, "testtemplate.doc"))

'Mail merge customer details from DataTable to the document.

'Whole document content will be repeated for each record.

Dim customers As DataTable = ExecuteDataTable("SELECT TOP 5 * FROM AsposeWordCustomers WHERE Country = 'USA'")

doc.MailMerge.Execute(customers)

End Sub

please help

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Most probably the error occurs because you don’t have implementation of ExecuteDataTable method in your project.

To perform selection from data base you should specify connection string, create connection, open connection, execute selection and close connection. For example see the following code:

'Here should be a connection string to your DB

'Create connction

Dim connectionString As String = "server=Web1;database=TestDB;uid=sa;pwd=yourpassword;"

Dim conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)

'Create DataSet

Dim ds As DataSet = New DataSet()

'Create sql command

Dim commandString As String = "SELECT * FROM Clients"

Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(commandString, conn)

'Create data adapter

Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(command)

'Open connection

conn.Open()

'Read dataset

adapter.Fill(ds)

conn.Close()

'Check if DataSet contains table

If (ds.Tables.Count > 0) Then

'Open template document

Dim doc As Document = New Document("in.doc")

'Execute mail merge

doc.MailMerge.Execute(ds.Tables(0))

'Save document

doc.Save("out.doc")

End If

I hope this could help you.

Best regards.