MS Office Forum / Word / Mailmerge and Fax / February 2004
How to programmatically use MailMerge in Word 2003?
|
|
Thread rating:  |
Ram - 19 Jan 2004 16:01 GMT I have a word document with all the Custom MailMerge fields defined. The file is saved as an XML document. I have data file for this document coming to me in an XML format which contains data for all the merge fields.
I would like to programmatically invoke Word 2003 from a .NET Application and apply the XML as the datasource, process the document and send to the printer.
Is it possible? If not, is there a better way to accomplish this?
Thanks, Ram
Cindy M -WordMVP- - 19 Jan 2004 19:03 GMT Hi Ram,
It's not possible for the very simple reason that Word can't use XML files as data sources for mail merge. If you consider very logically how complexly an XML file can be set up, then it makes sense the MS developers would say Word couldn't do this. Of course, the "normal user" looks at an XML file/data source very much as a delimited text file - it wouldn't contain this complexity...
Anyhow, that's the situation. So you'd need an alternate approach. First thing I'd check, however, is whether you can actually open this Word document you've saved as "XML" and perform a mail merge with it. Since there are different "flavors" of XML when one discusses Word 2003, it's important to assure what you want will work before you go any further.
If it does work, then I'd say use a transform on your XML data file to create a delimited text file. Use the OpenDataSource method to link the merge document to that, and off you go.
The other approach would be to: - don't use mail merge at all - the Word file should be a TEMPLATE - use the Documents.Add method to create a new document for each record you want to process - use DOM to drop the data into the document
> I have a word document with all the Custom MailMerge fields defined. > The file is saved as an XML document. I have data file for this [quoted text clipped - 6 lines] > > Is it possible? If not, is there a better way to accomplish this? Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Ram - 23 Jan 2004 17:27 GMT Thanks Cindy.
I did verify that if I transfrom my XML data to a delimited file, and provide that as the datasource, Mail Merge works fine.
Now, can you point me to place where I can see how to do this programmtically from a .NET Application?
Basically, I want to load this Document, set the datasource to the delimited file, save it and send to printer.
Can you help?
Thanks, Ram
Cindy M -WordMVP- - 23 Jan 2004 19:48 GMT Hi Ram,
> I did verify that if I transfrom my XML data to a delimited file, and > provide that as the datasource, Mail Merge works fine. > > Now, can you point me to place where I can see how to do this > programmtically from a .NET Application? > Do which? What you describe below, or convering the XML to a delimited file?
If the below, could you please record making the connection to the text file in Word (Tools/Macro/Record) so that I can see what the connection information your text file needs for the OpenDataSource method?
> Basically, I want to load this Document, set the datasource to the > delimited file, save it and send to printer. Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Ram - 28 Jan 2004 16:10 GMT Following is the code which running on the server to perform MailMerge programatically(see code below).....
The problem I am facing is that, the MailMerged document needs to be saved and I want to control the fileName and location to where it is getting saved. And later print it. How do I accomplish that?
- Ram
************************************************ Microsoft.Office.Interop.Word.Application wrdApp; Microsoft.Office.Interop.Word.Document wrdDoc; //Create an instance of Word, and make it invisible wrdApp = new Microsoft.Office.Interop.Word.Application(); wrdApp.Visible = false; //Open the template object strFileName = "C:\\Test\\TestDoc.doc"; object optional = Missing.Value; wrdDoc = wrdApp.Documents.Open(ref strFileName, ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref optional,ref optional, ref optional,ref optional,ref optional,ref optional, ref optional,ref optional,ref optional);
//set the Data File string strDataFileName = "C:\\Test\\TestDataFile.CSV"; wrdDoc.MailMerge.OpenDataSource(strDataFileName, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional); //Perform Merge wrdDoc.MailMerge.Execute(ref optional); object blnSave = false; wrdDoc.Close(ref blnSave, ref optional, ref optional); wrdApp.Quit(ref optional, ref optional, ref optional); ****************************************************
Cindy M -WordMVP- - 03 Feb 2004 23:23 GMT Hi Ram,
> Following is the code which running on the server to perform MailMerge > programatically(see code below)..... > > The problem I am facing is that, the MailMerged document needs to be > saved and I want to control the fileName and location to where it is > getting saved. And later print it. How do I accomplish that? Right, thanks for showing me where you are.
After the merge has executed, and you've closed wrdDoc the merge result should still be open as a document. You might want to set the wdApp.Visible = True while you work your way through this, just so that you can double-check what's going on :-)
So, what you'd need to do is something like this (I've left out most of the arguments): Microsoft.Office.Interop.Word.Document wrdMergeResult wrdMergeResult = wrdApp.ActiveDocument wrdMergeResult.SaveAs "Filepath and name" wrdMergeResult.PrintOut wrdMergeResult.Close
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Ram - 04 Feb 2004 16:14 GMT Thanks Cindy. That was very helpful
In your Message 2 in the Thread, you had mentioned about an alternate approach to solve the problem
>The other approach would be to: >- don't use mail merge at all >- the Word file should be a TEMPLATE >- use the Documents.Add method to create a new document for each record
>you want to process >- use DOM to drop the data into the document Can you explain further on how to accomplish that? You can use the same example I have mentioned in the earlier thread.
Thanks, Ram
Cindy M -WordMVP- - 04 Feb 2004 18:49 GMT Hi Ram,
> In your Message 2 in the Thread, you had mentioned about an alternate > approach to solve the problem [quoted text clipped - 8 lines] > Can you explain further on how to accomplish that? You can use the > same example I have mentioned in the earlier thread. DOM should be COM (sorry about that), which may make more sense :-)
The "target points" in the document you're creating can be various things, but most commonly bookmarks are used. When setting up the template, insert bookmarks instead of merge fields.
In your controlling application, create a data source in memory (using ADO.NET, for example [with which I'm not familiar, so forgive me if my sample code looks suspiciously like a mixture of DAO and ADO] :-))
The code for the Word stuff would roughly be as follows. I'm assuming you've already taken care of creating the data source, for which I'll use rs (recordset)
Microsoft.Office.Interop.Word.Application wrdApp; Microsoft.Office.Interop.Word.Document wrdDoc; Microsoft.Office.Interop.Word.Range wrdRng //Create an instance of Word, and make it invisible wrdApp = new Microsoft.Office.Interop.Word.Application(); wrdApp.Visible = false; //Open the template object strFileName = "C:\\Test\\TestDoc.dot"; object optional = Missing.Value; wrdDoc = wrdApp.Documents.Add(ref strFileName);
object strBookmarkName = rs.Fields.Item(1).Name object strData = rs.Fields.Item(1).Value //We recreate the bookmark around the data so that //it can be targeted again, if desired //Note: I usually make this a separate function If wrdDoc.Bookmarks.Exists(strBookmarkName) Then wrdRng = wrdDoc.Bookmarks(strBookmarkName).Range wrdRng.Text = strData wrdDoc.Bookmarks.Add(strBookmarkName, wrdRng) End If
wrdDoc.Close(ref blnSave, ref optional, ref optional); wrdApp.Quit(ref optional, ref optional, ref optional); ****************************************************
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Ram - 13 Feb 2004 22:59 GMT Cindy:
I was reading through the article...
http://msdn.microsoft.com/office/understanding/xmloffice/gettingstarted/default. aspx?pull=/library/en-us/dno2k3ta/html/odc_xmlinoffice2003_summarydoc.asp
In this article, right below Figure 2, the author says... "In addition, using smart tags or smart documents, developers can attach logic to specific XML elements within the document. For example in the figure above, Contoso could build a solution to aid their salespeople when filling out the Receipt of Samples Form shown in the figure above. In such a solution, a custom-built DLL could retrieve the doctor's information from Contoso's internal records and automatically insert it into the proper fields based on the XML tags within the document."
With the use of "XML Toolbox for Word", I can convert the XML tags in the documents to Nodes. My question is that, can these Nodes to be easily replaced by providing the XML file containing the data programmatically? (something like MailMerge.Execute()). Or is the author talking about programatically replacing the XML tags one by one like what you had mentioned earlier "using bookmarks"?
Ram
Cindy M -WordMVP- - 19 Feb 2004 16:10 GMT Hi Ram,
> In addition, using smart tags or smart documents, developers can > attach logic to specific XML elements within the document. Just so we're clear: anything about smart docs is not mail-merge related. When the user puts the focus in a document within an XML tag, the "Document Actions" task pane could change, and code could be executed. But it's all user-interactive, so isn't really relevant to your question.
> With the use of "XML Toolbox for Word", I can convert the XML tags in > the documents to Nodes. My question is that, can these Nodes to be > easily replaced by providing the XML file containing the data > programmatically? (something like MailMerge.Execute()). Or is the > author talking about programatically replacing the XML tags one by one > like what you had mentioned earlier "using bookmarks"? I'm not familiar with the "XML toolbox" (do you have a URL?), but generally I can tell you that you should be able to assign value to XML tags/nodes. I'm not sure why you'd prefer to do that in the Word UI, however, since working with Word is generally slower than manipulating a pure text file. It would almost certainly be faster to use xslt and dom to "merge" data into an XML "structure", then open and print that in Word.
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
|
|
|