Free Support Forum - aspose.com

How To Setup Word Doc

I’m having a difficult time understanding how to get a word document setup so I can insert fields from the database.

Let’s say I want to insert CustomerLastName into a document. I’m using this line of code:
Doc.MailMerge.ExecuteWithRegionsADO RS, "qryCurrentCustomer"

How do I put into the document:
?TableStart:qryCurrentCustomer?
?CustomerLastName?
?TableEnd:qryCurrentCustomer?

Do I have to start a mail merge with the query? When I did that all I got was an option to insert ?CustomerLastName? into the document but I how do I instert the TableStart and TableEnd fields?

Your code is correct. A mail merge is performed when you invoke Document.MailMerge.ExecuteWithRegionsADO method. You should pass populated recordset to this method. TableStart and TableEnd fields are used to mark beginning and end of a mail merge region only.

Also, make sure you do need mail merge regions for your task. You can mail merge without regions as well. The demo project illustrates the difference.

How do you inster the TableStart and TableEnd fields in the document?

I figured it out that if you’re not using regions you do this:
Doc.MailMerge.ExecuteADO RS

However, now when I run the code it just creates a new empty document, what does that mean?

You do not need to use TableStart and TableEnd at all when you perform a simple mail merge without regions. But you still do need to use Word MERGEFIELD and optionally NEXT fields.
It seems like your document doesn't contain any merge fields because if a field name that is not found in the document is encountered, it is ignored.

So I guess “Doc.MailMerge.ExecuteADO RS” isn’t enough?

If I just have one field called “CustomerLastName” in the doc, what is the syntax to use Mergefield?

In order to insert a merge field named “CustomerLastName”, select “Field” from the “Insert” dropdown menu in MS Word. Then choose “MergeField” from the list and give the field “CustomerLastName” name.
You can press Alt+F9 to toggle display of field codes on or off. In your case, the code of the merge field looks like { MERGEFIELD CustomerLastName * MERGEFORMAT }.
Then you can populate the field with “Doc.MailMerge.ExecuteADO RS”.

Please have a look at this page in the documentation too http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.MailMerge.ExecuteWithRegions1.html

When I go to Insert -> Field -> MergeField, I get a list of choices on the right called Field Names, which one do I choose? I can’t just use the Insert Merge Field button and choose the field?

I see, you go to Insert -> Field and select “Mail Merge” from the left list and select “MergeField” from the right list. Then leave the “MERGEFIELD” in there and type your field name so I typed “CustomerLastName”. So now the document has:
{ MERGEFIELD CustomerLastName * MERGEFORMAT }

The next problem is that when I run my ASP page, it generates a new document but it’s completely blank.

Please send the document template you have created (before it is processed by Aspose.Word) and fragment of your code intended to populate the fields in the document to word@aspose.com. We’ll have a look at it and help you to solve the problem.

I can still send it if you want but I think I found the source of the problem. Even though Microsoft Query is returning results, if I manually run a Mail Merge in Word, the field turns up blank! I’m using an ODBC source that connects to a SQL Server 2000 database. Do you know why it does this? As a test, I connected to a mdb and it worked just fine just to make sure I’m doing things correctly.

Please check your ODBC connection string. Make sure that the recordset is properly populated before passing to Document.MailMerge.ExecuteADO method.

Yes, the fields will turn out blank if they are found in the recordset but contain no records or no data for a record.

Well MS Query returns records but when I merge it in Word they show up blank…???

However, please make sure programmatically that your recordset contains records because MS Query could have other connection settings.