1 Reply Latest reply on Aug 31, 2017 6:19 AM by BKBK

    Merge query or cfset data with word docx or doc and download the updated version

    weezerboy Level 1

      I have a word docx named coded_word.doc that is coded (written)  like this

       

      %COURTESY_TITLE% %FIRST_NAME% , %LAST_NAME%

       

      Dear %COURTESY_TITLE% %LAST_NAME%,

       

      ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------

      I have cf code like this

      <!---Read the file--->

      <cfset mydoc= FileRead("#GetDirectoryFromPath(GetCurrentTemplatePath())#coded_word.docx") />

       

      <!---Replace the coded variables in he word doc withthese values. These ultimately will be from a query result--->

      <cfset mydoc= Replace(rtf,"%COURTESY_TITLE%","Mr.") />

      <cfset mydoc= Replace(rtf,"%FIRST_NAME%","Charles") />

      <cfset mydoc= Replace(rtf,"%LAST_NAME%","Jones") />

       

      <!---Then download the modified file--->

      <CFHEADER NAME="content-disposition" VALUE="attachment; filename=mydoc_updated.doc">

      <cfcontent type="application/vnd.ms-word" file="#GetDirectoryFromPath(GetCurrentTemplatePath())#mydoc_updated.doc">

       

      But when I do I get the original word doc. I don't get  mydoc_updated.doc.

       

      I want to merge/replace the %variable% with actual/set values and need the updated version mydoc.doc to download like this

       

      Mr. Charles Jones

       

      Dear Mr. Jones

       

      QUESTIONS

      1. Do I have the coding correct in the word doc?
      2. Do I need to save and rename the new file before downloading?
      3. Can I use .docx extension or does that create additional issues as I probably can't use <cfcontent type="application/vnd.ms-word" ?
      4. I need the original coded_word.doc to stay the same as it is a template that I'll need for the next query result.
        • 1. Re: Merge query or cfset data with word docx or doc and download the updated version
          BKBK Adobe Community Professional & MVP

          weezerboy  wrote

           

          QUESTIONS

          1. Do I have the coding correct in the word doc?

          Probably not. You're reading mydoc in, but replacing in rtf.

           

          In any case, be aware that ColdFusion is reading and storing mydoc as a binary. So you just can't replace strings within it.

           

          2. Do I need to save and rename the new file before downloading?

          Not necessarily. But this question is irrelevant, as you have to address the others first.

           

          3. Can I use .docx extension or does that create additional issues as I probably can't use <cfcontent type="application/vnd.ms-word" ?

          The extension .docx is fine, assuming you have a recent version of ColdFusion. However, its MIME type is application/vnd.openxmlformats-officedocument.wordprocessingml.document.

           

          4. I need the original coded_word.doc to stay the same as it is a template that I'll need for the next query result.

          It may not be as simple as that. As you can see, the MIME type hints at XML. (To demonstrate, extract the docx as a zip file) .You might have to decompose the docx into its XML constituent parts, make the replacements, then recompose the result back to docx.