July

8

Using ColdFusion to Generate Multiple Word Documents (Batch Creation)

UPDATE: Now includes line returns. The example’s form has been modified with additional fields so adding line returns to the Word doc could be shown. The zip has all the new files.

As a continuation (and request from Simon) to the Using ColdFusion to Generate a Custom Word Document post, here is an example of creating multiple Word documents in a batch-like manner.

Create the Word template as an RTF (.rtf) document with placeholders in the same manner as the previous post. Collect the data or have it spit out from a database.

Here’s where the code changes for multiple file generation. The number of files to be created is looped over, a dynamic name is generated for the new file, the output of the Replace functions is written to the new file, and the file is renamed with a .doc extension.

Also, a cfloop was added for the studentname fields so a line return (\par) could be appended on.

<cfset pathToRTF =  GetDirectoryFromPath(GetCurrentTemplatePath()) & "homeworkpass.rtf" />

<cfloop from="1" to="10" index="i">

    		<cfset studentnames = "" />

                <cfloop from="1" to="3" index="num">
                	<cfset studentnames = studentnames & " " & form["studentname" & num] & " \par " />
                </cfloop>

            <cflock name="homeworkpass" type="exclusive" timeout="30">

                <cfif form.cfversion EQ "pre8">
                <!--- CFMX7 or earlier --->
                    <cffile action="read" file="#pathToRTF#" variable="rtf">
                 <cfelse>
                <!--- CF8 or later --->
                    <cfset rtf = FileRead(pathToRTF) />
                </cfif>

                <cfset rtf = Replace(rtf,"%expirydate%",form.expirydate) />
                <cfset rtf = Replace(rtf,"%points%",form.points) />
                <cfset rtf = Replace(rtf,"%studentname%",studentnames) />
                <cfset rtf = Replace(rtf,"%subject%",form.subject) />
                <cfset rtf = Replace(rtf,"%datereceived%",form.datereceived) />

            </cflock>

            <cfset rtfFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "classPasses/" & "homeworkpass" & i & ".rtf" />
            <cfset docFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "classPasses/" & "homeworkpass" & i & ".doc" />

            <cfif form.cfversion EQ "pre8">
                <!--- CFMX7 or earlier --->
                    <cffile action="write" file="#rtfFile#" output="#rtf#" />
            <cfelse>
                <!--- CF8 or later --->
                    <cfset FileWrite(rtfFile,rtf) />
             </cfif>

             <cffile action="rename" source="#rtfFile#" destination="#docFile#" />

         </cfloop>
         <p>Passes Generated</p>
         <cfabort>

This should get you pointed in the right direction for creating multiple Word documents. And, again, there is a choice of ColdFusion 8 or an earlier version just to show the new, simpler functions available in CF8.

Demo

Download Files (zip)

2 Comments for Using ColdFusion to Generate Multiple Word Documents (Batch Creation)

[...] As a continuation (and request from Simon) to the Using ColdFusion to Generate a Custom Word Document post, here is an example of creating multiple Word documents in a batch-like manner. Create the Word template as an RTF (.rtf) document with placeholders in the same manner as the previous post . Original post: Using ColdFusion to Generate Multiple Word Documents (Batch Creation) [...]


Simon Bingham
July 9, 2009

Hi Jen,

Thank you very much for the code and all your help! Much appreciated.

Simon

Leave a comment

Why ask?

 

« | »