• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1019
  • Last Modified:

Writing data dynamically into a Word documnet using ColdFusion

Experts,

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.
0
Tpaul_10
Asked:
Tpaul_10
  • 6
  • 5
1 Solution
 
_agx_Commented:
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

http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/

2) Use Word's ability to parse html. Just generate html and use cfheader/cfcontent
to tell the browser it's a word document.
0
 
Tpaul_10Author Commented:
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.

Thanks
0
 
_agx_Commented:
Ok. Then option #1 ie .rtf should be fine.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Tpaul_10Author Commented:
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)

Thanks
Appreciate any help on #2 since others are ok.
0
 
_agx_Commented:
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?
0
 
Tpaul_10Author Commented:
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
0
 
_agx_Commented:
(Update)

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

       "application/vnd.openxmlformats-officedocument.wordprocessingml.document"


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.

http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
0
 
Tpaul_10Author Commented:
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
0
 
_agx_Commented:
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?
0
 
Tpaul_10Author Commented:
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.

Thanks
0
 
_agx_Commented:
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 --->
           <html><head>
           <script type="text/javascript">
                 function redirectToDownloadPage() {
                      // call download page (with parameters) here
                      // ie window.location.href = "downloadPage.cfm?param1=xxx&param2=...";
                 }
           </script></head>
           <body onLoad="redirectToDownloadPage()">
                  <strong> File generated. If your download doesn't start in [X] seconds
                  <cfoutput>
                  <a href="downloadPage.cfm?param1=#form.param1#&param2=...">click here </a>
                  </cfoutput>
           </body>
           </html>
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now