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.

Download Files (zip)

 

Further Reading:

  1. Using ColdFusion to Generate a Custom Word Document

6 Comments

  • Pingback: Using ColdFusion to Generate Multiple Word Documents (Batch Creation) | Adobe Tutorials

  • July 9, 2009 - 3:36 am | Permalink

    Hi Jen,

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

    Simon

  • May 10, 2010 - 12:45 pm | Permalink

    Hi Jen,

    I have been playing with your Word/CF code and have it working locally…thanks!

    Curious how you would combine 2 multi-page documents into 1. Also do you know if it would handle the page numbering?

  • May 10, 2010 - 12:59 pm | Permalink

    @Ebud
    ColdFusion and Word is kind of kludge all by itself. So, no, I don't know how to combine the docs or use the page numbering. Wish I could help.

  • Jim
    August 23, 2010 - 8:17 am | Permalink

    Hey Jen,

    This works great. One question, is it possible to have these open directly in Word, rather than writing the files? Since I am working in a Corporate environment the servers are pretty well locked down and I am not able to dynamically create new files on the server. Ideally, I would like my code to loop through and open a document in Word for each record in my loop. Thanks in advance for any help.

    Jim

  • September 10, 2010 - 7:54 am | Permalink

    @Jim
    Not possible that I know of. Coldfusion and Word is a difficult combination. They don't play nice.

  • Comments are closed.