C# mailmerge of template.doc with SQL Server database table

J

Jeff Cochand

Hi,
I have what I think is a very basic mailmerge question.

I want to write a C# program that merges myTemplate.doc with a db
table, tablePeople. The result will be a multipage word doc that can
be printed out.



The user will simply push a button in the C# program, resulting in all
the first names in the SQL Server table, tablePeople, merging with the
word doc, myTemplate.doc.

myTemplate.doc looks something like this:
Dear <<firstName>>
Please read this doc.
Sincerely, Jeff

tablePeople has several rows of firstName, lastName, address, etc.

I have http://support.microsoft.com/default.aspx?scid=kb;EN-US;301659
running on my system, but I don't have a general understanding of
programmatically merging documents. Is there a good overview of this
somewhere?

• Can I simply use an already created template doc, rather than
creating one on the fly?
• How does the template doc get associated with the datasource?
• wrdSelection.Range – Is this the key setting indicating that the
merge process should look in the datasource & template file for
whatever the parameter is set to: (ie
wrdMergeFields.Add(wrdSelection.Range, "FirstName"); )?
• wrdApp.Selection – Is this the data source? In this case it's the
same word doc as we write to? Could this be a database?
• wrdDoc.MailMerge – Is this the MS Word doc where the resulting
merged document will be written?

Thanks,
Jeff
 
D

Doug Robbins

I would suggest that you open the Visual Basic Editor in Word and type in
the word mailmerge and then press F1 to bring up the help on the subject.
That will hopefully allow you to correct your understanding of some of the
terms.

It sounds like you already have a template with the necessary mailmerge
field <<name>> in it. It need not actually be a template, it could just be
a Word Document (in this case it's the mailmerge main document which
admittedly is something similar to a template because when the merge is
executed, a new document is created and there is no change to the main
document.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Jeff Cochand

Thanks for your response.

I read over the Visual Studio/VB mailmerge help. I tested some ideas
out & searched around for more ideas. I'm surprised I can't create or
find a simple example.

It seems I simply want to do this:
- Open a 'template' doc
- Open a datasource from a db
- Run mailmerge

Could you throw in a few lines of c# sample code that demonstrates
that?
 
D

Doug Robbins

Sorry, I know zilch about C# If when you say Visual Studio/VB mailmerge
help, you mean what I think you do, then that would be the wrong place to
look. Open the Visual Basic editor in Word (Alt+F11) and type mailmerge and
then press F1 to get the VBA help on mailmerge.

Maybe a C# newsgroup would be a better place to ask.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top