Merge query or cfset data with word docx or doc and download the updated version
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
- Do I have the coding correct in the word doc?
- Do I need to save and rename the new file before downloading?
- Can I use .docx extension or does that create additional issues as I probably can't use <cfcontent type="application/vnd.ms-word" ?
- 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.
