<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jensbits.com &#187; Word</title>
	<atom:link href="http://www.jensbits.com/tag/word/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jensbits.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 03:44:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using ColdFusion to Generate Multiple Word Documents (Batch Creation)</title>
		<link>http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/</link>
		<comments>http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 14:34:40 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=257</guid>
		<description><![CDATA[UPDATE: Now includes line returns. The example&#8217;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 [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/' rel='bookmark' title='Permanent Link: Using ColdFusion to Generate a Custom Word Document'>Using ColdFusion to Generate a Custom Word Document</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE: Now includes line returns. The example&#8217;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.</strong></p>
<p>As a continuation (and request from Simon) to the <a href="/2009/06/21/using-coldfusion-to-generate-a-word-document/">Using ColdFusion to Generate a Custom Word Document</a> post, here is an example of creating multiple Word documents in a batch-like manner.</p>
<p>Create the Word template as an RTF (.rtf) document with placeholders in the same manner as the <a href="/2009/06/21/using-coldfusion-to-generate-a-word-document/">previous post</a>. Collect the data or have it spit out from a database. </p>
<p>Here&#8217;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.</p>
<p>Also, a cfloop was added for the studentname fields so a line return (\par) could be appended on.</p>
<pre class="brush: coldfusion;">
&lt;cfset pathToRTF =  GetDirectoryFromPath(GetCurrentTemplatePath()) &amp; &quot;homeworkpass.rtf&quot; /&gt;

&lt;cfloop from=&quot;1&quot; to=&quot;10&quot; index=&quot;i&quot;&gt;

    		&lt;cfset studentnames = &quot;&quot; /&gt;

                &lt;cfloop from=&quot;1&quot; to=&quot;3&quot; index=&quot;num&quot;&gt;
                	&lt;cfset studentnames = studentnames &amp; &quot; &quot; &amp; form[&quot;studentname&quot; &amp; num] &amp; &quot; \par &quot; /&gt;
                &lt;/cfloop&gt;

            &lt;cflock name=&quot;homeworkpass&quot; type=&quot;exclusive&quot; timeout=&quot;30&quot;&gt;

                &lt;cfif form.cfversion EQ &quot;pre8&quot;&gt;
                &lt;!--- CFMX7 or earlier ---&gt;
                    &lt;cffile action=&quot;read&quot; file=&quot;#pathToRTF#&quot; variable=&quot;rtf&quot;&gt;
                 &lt;cfelse&gt;
                &lt;!--- CF8 or later ---&gt;
                    &lt;cfset rtf = FileRead(pathToRTF) /&gt;
                &lt;/cfif&gt;

                &lt;cfset rtf = Replace(rtf,&quot;%expirydate%&quot;,form.expirydate) /&gt;
                &lt;cfset rtf = Replace(rtf,&quot;%points%&quot;,form.points) /&gt;
                &lt;cfset rtf = Replace(rtf,&quot;%studentname%&quot;,studentnames) /&gt;
                &lt;cfset rtf = Replace(rtf,&quot;%subject%&quot;,form.subject) /&gt;
                &lt;cfset rtf = Replace(rtf,&quot;%datereceived%&quot;,form.datereceived) /&gt;

            &lt;/cflock&gt;

            &lt;cfset rtfFile = GetDirectoryFromPath(GetCurrentTemplatePath()) &amp; &quot;classPasses/&quot; &amp; &quot;homeworkpass&quot; &amp; i &amp; &quot;.rtf&quot; /&gt;
            &lt;cfset docFile = GetDirectoryFromPath(GetCurrentTemplatePath()) &amp; &quot;classPasses/&quot; &amp; &quot;homeworkpass&quot; &amp; i &amp; &quot;.doc&quot; /&gt;

            &lt;cfif form.cfversion EQ &quot;pre8&quot;&gt;
                &lt;!--- CFMX7 or earlier ---&gt;
                    &lt;cffile action=&quot;write&quot; file=&quot;#rtfFile#&quot; output=&quot;#rtf#&quot; /&gt;
            &lt;cfelse&gt;
                &lt;!--- CF8 or later ---&gt;
                    &lt;cfset FileWrite(rtfFile,rtf) /&gt;
             &lt;/cfif&gt;

             &lt;cffile action=&quot;rename&quot; source=&quot;#rtfFile#&quot; destination=&quot;#docFile#&quot; /&gt;

         &lt;/cfloop&gt;
         &lt;p&gt;Passes Generated&lt;/p&gt;
         &lt;cfabort&gt;
</pre>
<p>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.</p>
<p id="demo"><a href="http://cf-jensbits.com/demos/cfwordbatch/" onclick="_gaq.push(['_link', 'http://cf-jensbits.com/demos/cfwordbatch/']); return false;"><span>Demo</span></a></p>
<p id="download"><a href="/media/code/CFgenerateWordMultiFiles.zip"><span>Download Files (zip)</span></a></p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/' rel='bookmark' title='Permanent Link: Using ColdFusion to Generate a Custom Word Document'>Using ColdFusion to Generate a Custom Word Document</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using ColdFusion to Generate a Custom Word Document</title>
		<link>http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/</link>
		<comments>http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 20:51:22 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=238</guid>
		<description><![CDATA[An oldie but a goodie, ColdFusion can be used to generate Word documents. Here is a quick example of writing to and opening a Word document. Create Word Template Create a Word template by adding &#8220;placeholders&#8221; in the document. This is done by wrapping a keyword with % (percent) signs as such: %firstname%. Put in [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/' rel='bookmark' title='Permanent Link: Using ColdFusion to Generate Multiple Word Documents (Batch Creation)'>Using ColdFusion to Generate Multiple Word Documents (Batch Creation)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>An oldie but a goodie, ColdFusion can be used to generate Word documents. Here is a quick example of writing to and opening a Word document.</p>
<h3>Create Word Template</h3>
<p>Create a Word template by adding &#8220;placeholders&#8221; in the document. This is done by wrapping a keyword with % (percent) signs as such: %firstname%. Put in as many as you want and add any necessary formatting in Word. Your replacement text will pick up the formatting. Save the file as an RTF (.rtf). For simplicity sake, put it in the same directory as the CF file used to generate it. Here an RTF file called homeworkpass.rtf is used.</p>
<h3>Collect the Data</h3>
<p>This example uses form fields to fill the document. Data from a database could be used just as easily. It&#8217;s a pretty standard form, nothing special.</p>
<pre class="brush: xml;">
&lt;form action=&quot;index.cfm&quot; method=&quot;post&quot;&gt;
    &lt;fieldset&gt;
    &lt;legend&gt;Homework Pass Info&lt;/legend&gt;
    &lt;p&gt;&lt;label for=&quot;expirydate&quot;&gt;Expiration Date:&lt;/label&gt;&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;expirydate&quot; name=&quot;expirydate&quot; value=&quot;#DateFormat(DateAdd('d',10,Now()),'mm/dd/yy')#&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;label for=&quot;points&quot;&gt;Points:&lt;/label&gt;&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;points&quot; name=&quot;points&quot; value=&quot;100&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;label for=&quot;studentname&quot;&gt;Student Name:&lt;/label&gt;&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;studentname&quot; name=&quot;studentname&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;label for=&quot;subject&quot;&gt;Subject:&lt;/label&gt;&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;subject&quot; name=&quot;subject&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;label for=&quot;datereceived&quot;&gt;Date Received:&lt;/label&gt;&lt;br /&gt;
    &lt;input type=&quot;text&quot; id=&quot;datereceived&quot; name=&quot;datereceived&quot; value=&quot;#DateFormat(Now(),'mm/dd/yy')#&quot; /&gt;&lt;/p&gt;
    &lt;/fieldset&gt;

    &lt;fieldset&gt;
    &lt;legend&gt;Version of ColdFusion:&lt;/legend&gt;
    &lt;p&gt;&lt;label&gt;&lt;input type=&quot;radio&quot; id=&quot;cfversion&quot; name=&quot;cfversion&quot; value=&quot;pre8&quot; /&gt;CFMX or earlier&lt;/label&gt;&lt;br /&gt;
    &lt;label&gt;&lt;input type=&quot;radio&quot; id=&quot;cfversion&quot; name=&quot;cfversion&quot; value=&quot;post8&quot; /&gt;CF8 or later&lt;/label&gt;&lt;/p&gt;
    &lt;/fieldset&gt;

    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Generate Word&quot; /&gt;&lt;/p&gt;
    &lt;/form&gt;
</pre>
<h3>Replace Text with ColdFusion</h3>
<p>After a little error checking, the file is opened and the placeholders are swapped out with the form field values using the Replace() function in the homeworkpass.rtf. </p>
<pre class="brush: coldfusion;">
&lt;cfset error = &quot;&quot; /&gt;

&lt;cfif isDefined(&quot;form.submit&quot;)&gt;

	&lt;cfloop collection=&quot;#form#&quot; item=&quot;i&quot;&gt;
		&lt;cfif NOT Len(form[i]) OR  (NOT isDefined(&quot;form.cfversion&quot;))&gt;
			&lt;cfset error = &quot;All fields must have a value&quot; /&gt;
		&lt;/cfif&gt;
	&lt;/cfloop&gt;

	&lt;cfif NOT Len(error)&gt;

    	&lt;cfset pathToRTF =  GetDirectoryFromPath(GetCurrentTemplatePath()) &amp; &quot;homeworkpass.rtf&quot; /&gt;        

        &lt;cflock name=&quot;homeworkpass&quot; type=&quot;exclusive&quot; timeout=&quot;30&quot;&gt;

			&lt;cfif form.cfversion EQ &quot;pre8&quot;&gt;
            &lt;!--- CFMX7 or earlier ---&gt;
                &lt;cffile action=&quot;read&quot; file=&quot;#pathToRTF#&quot; variable=&quot;rtf&quot;&gt;
             &lt;cfelse&gt;
            &lt;!--- CF8 or later ---&gt;
                &lt;cfset rtf = FileRead(pathToRTF) /&gt;
            &lt;/cfif&gt;

            &lt;cfset rtf = Replace(rtf,&quot;%expirydate%&quot;,form.expirydate) /&gt;
            &lt;cfset rtf = Replace(rtf,&quot;%points%&quot;,form.points) /&gt;
            &lt;cfset rtf = Replace(rtf,&quot;%studentname%&quot;,form.studentname) /&gt;
            &lt;cfset rtf = Replace(rtf,&quot;%subject%&quot;,form.subject) /&gt;
            &lt;cfset rtf = Replace(rtf,&quot;%datereceived%&quot;,form.datereceived) /&gt;

        &lt;/cflock&gt;

        &lt;cfheader name=&quot;content-disposition&quot; value=&quot;filename=HomeworkPass.doc&quot; /&gt;

    	&lt;cfcontent type=&quot;application/msword&quot;&gt;&lt;cfoutput&gt;#rtf#&lt;/cfoutput&gt;
    	&lt;cfabort&gt;

	&lt;/cfif&gt;

&lt;/cfif&gt;
</pre>
<p>There is a section here to allow for ColdFusion version specification. For ColdFusion 8 the FileRead() function can be used. All other versions of CF will need to use cffile.</p>
<pre class="brush: coldfusion;">
&lt;cfif form.cfversion EQ &quot;pre8&quot;&gt;
    	&lt;!--- CFMX7 or earlier ---&gt;
    		&lt;cffile action=&quot;read&quot; file=&quot;#pathToRTF#&quot; variable=&quot;rtf&quot;&gt;
   		 &lt;cfelse&gt;
    	&lt;!--- CF8 or later ---&gt;
    		&lt;cfset rtf = FileRead(pathToRTF) /&gt;
    	&lt;/cfif&gt;
</pre>
<p>Offering a choice for ColdFusion will not be necessary in production; it&#8217;s here for illustrative purposes only. At this time all versions of Word can open .doc files.</p>
<h3>Output the Word File</h3>
<p>To output the Word file use cfcontent and stop processing the rest of the page.</p>
<pre class="brush: coldfusion;">
&lt;cfcontent type=&quot;application/msword&quot;&gt;&lt;cfoutput&gt;#rtf#&lt;/cfoutput&gt;
    	&lt;cfabort&gt;
</pre>
<p id="demo"><a href="http://cf-jensbits.com/demos/cfword/" onclick="_gaq.push(['_link', 'http://cf-jensbits.com/demos/cfword/']); return false;"><span>Demo</span></a></p>
<p id="download"><a href="/media/code/CFgenerateWord.zip"><span>Download Files (zip)</span></a></p>
<p>To create multiple Word documents in a batch-like process see the <a href="/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/">Using ColdFusion to Generate Multiple Word Documents (Batch Creation)</a> post.</p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/' rel='bookmark' title='Permanent Link: Using ColdFusion to Generate Multiple Word Documents (Batch Creation)'>Using ColdFusion to Generate Multiple Word Documents (Batch Creation)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/06/21/using-coldfusion-to-generate-a-word-document/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
