We help IT Professionals succeed at work.

Writing data dynamically into a Word documnet using ColdFusion

Tpaul_10 asked
Medium Priority
Last Modified: 2012-09-06

I have a form, where users will enter the product number and when the user hits the submit button/get the details button, I should open up the product details in a word document (I should say pre-defined word document where they have the titles/headlines for the product in the word document, like Product Name:, Product Number:, Address:, etc) from the SQL query or in other words I should generate the data into the word document dynamically.

I am not sure if this is possible. If so, could you please help me out how to do it with an example and your help is greatly appreciated.

Thanks and appreciate your help.
Please let me know if you need any further information.
Watch Question

Most Valuable Expert 2015
Do you need to do anything else with the "document" other than displaying it?

Generating a "real" Word document is involved. Two simpler options are:
1) Generate an *.rtf file instead. Use placeholders for the product name, etc..  Then use replace() to substitute the query values


2) Use Word's ability to parse html. Just generate html and use cfheader/cfcontent
to tell the browser it's a word document.


Thanks for the reply Agx.

I don't need to do anything else with the document other than displaying the details and open the document.

I will read thorugh the documnet and will let you know if I have any questions.

Most Valuable Expert 2015

Ok. Then option #1 ie .rtf should be fine.


Thanks again and I am trying to complete this, but here are the few things I have seen.

1. The dailoge box to open/save the documnet says "Type: Microsoft Office Word 97-2003" eventhough I have .docx in the code below.
<cfheader name="content-disposition" value="filename=myProdDocument.docx" />
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>

2.When I open the documnet from the dailoge box, the name of the file on top of the documnet says (index.cfm (Read-Only)[compatibility Mode] - Microsoft Word
Why is this?
This is hapeening with the example you have provided as well.

3.When I try to "save as" after opening the document, it tries to saves as index.rtf.
But If I try to save as from the dailoge box without opening the documnet, it saves as the mydocumnet.doc (which is ok)

Appreciate any help on #2 since others are ok.
Most Valuable Expert 2015

I noticed your code is using extension ie ".docx" which doesn't match the older mime type for *.doc files ie "application/msword".   Try fixing that first.  

Also what version of ms office and CF are you running?


I am using MS Office 2007 and CF 9.0.

Appreicate if you can let me know the mime type for .docx or the newer version of office?
I will read or google to find as well -:)

Thanks for your help
Most Valuable Expert 2015


It's a mouthful. But I'd try using the original *.doc extension (or *rtf) first.


Regarding #2,  when I've seen that happen it's usually because I have the same document or doc name open somewhere else. Closing the duplicates makes it go away.

             > I am using MS Office 2007

One thing to watch out for with 2007+ is the new extension hardening features.  It warns when the file extension and mime type don't match.  I don't remember offhand if this combination also triggers the warning.  If so try  *.rtf and "text/rtf" instead.



Thanks for the update Agx.

I have another issue, after the code for opening the word documnet, I have <cflocation> tag to take the user to a message page (your documnet has been generated successfully).

The word document doesn't open with <cflocation> code, but just goes to the message page, if I take off the <cflocation> tag code, then it works/open up the documnet with a dialogue box.

Any inputs on that?

here is the code I have
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>

<cflocation url="index.cfm?fuseaction=errorMessage&errorNum=#errorNum#" addtoken="no">

Thanks and appreicate all your help
Most Valuable Expert 2015

Right, you can't have a download and cflocation on the same page because they're mutually exclusive. You can do one or the other - but not both.  The job of cflocation is to send the user to a different page - before the page is generated. So when you add a cflocation, the download is essentially skipped.

I'm not sure I see the point of displaying a confirmation for a download. As the user can tell it succeeded when the download prompt appears :)  Is there some other problem you're trying to solve?


LOL, thanks for the clarification.

I am adding some validations based on the data entered by the user and sending them to a error message page, though it would be nice to send them to a confirmation page since I am not clearing the form fields and not trying anything else.

Most Valuable Expert 2015

That's possible, but you'll need client side scripting ie javascript.  

You basically have 3 pages:

       1) theForm.cfm                       <=== data entry and validation
       2) confirmPage.cfm                 <== displays message & redirects to download
       3) downloadPage.cfm               <== generates file w/cfheader+cfcontent

Page 2 is the key.  It displays a success message and redirects to the download page. It's very similar to what you were trying to do with cflocation. The difference is it uses client side scripting. So the browser displays the html first - then does the download.

There's slicker ways to do this, but the basic flow of page 2 is:

Edited example

           <!--- psuedo code --->
           <script type="text/javascript">
                 function redirectToDownloadPage() {
                      // call download page (with parameters) here
                      // ie window.location.href = "downloadPage.cfm?param1=xxx&param2=...";
           <body onLoad="redirectToDownloadPage()">
                  <strong> File generated. If your download doesn't start in [X] seconds
                  <a href="downloadPage.cfm?param1=#form.param1#&param2=...">click here </a>