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.
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.
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”.
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.