Hi List, I've had a few requests off-list for the code on how I print out pages through Word. I'll go ahead and post it for everyone's benefit. Please feel free to write me on or off list if you have any problems. To start, you need to have Word installed on your CF server for this unless you use DCOM - I'm not familiar with DCOM so this is what I have: This is used best when you have output pages that change significantly from user to user. See below if you need to do something along the lines of a mail merge document. First, I generate the HTML page on the fly as if it were being output to the browser and store it in the variable 'page': <cfset page = '<html><head><style></style></head><body>#STUFF#</body></html>'> Next, I write it to a file: <cffile action="write" file="c:\inetpub\web_documents\output.htm" output="#page#"> Next, I open it up in Word via CFOBJECT - COM and print it out. <cftry> <cfobject action="CONNECT" class="Word.Application" name="objWord" type="COM"> <cfcatch> <cfobject action="CREATE" class="Word.Application" name="objWord" type="COM"> </cfcatch> </cftry> <cftry> <cfscript> objDoc = objWord.Documents; newDoc = objDoc.open("C:\inetpub\web_documents\output.htm"); // Your filename goes here switch (form.printer) { // If you have multiple printer you want to print to. case 1: {objWord.ActivePrinter = "HP LaserJet 1200 Series PCL"; break;} // Local printer case 2: {objWord.ActivePrinter = "\\lana\HP LaserJet 4050 Series PCL"; break;}} // Network pr. po = newDoc.PageSetup; // Get the page setup object po.Orientation = 1; // Set orientation to landscape. Omit for normal. po.TopMargin = 36; // Set margins - this Is not necessary po.BottomMargin = 36; po.LeftMargin = 36; po.RightMargin = 36;
newDoc.PrintOut(); // Print the document newDoc.Close(); // Close the Document </cfscript> <cfcatch> <cfscript> newDoc.Close(); // Catch errors and close the document and application if caught objWord.Quit(); </cfscript> <cfrethrow> </cfcatch> </cftry> I leave the application (Word) open because if it closes before the printing is done, then a dialog box pops up on the server and it must be attended to before the printing continues. I don't think that Office XP has this problem but I use 2000. I think that if you have a template that you want to print to - like form letters or anything where only the same amount of information changes - you might be better off writing a word document that prints them out from a mail merge document, then call that document to print: First, make up a table that will contain the information that you want to have printed on the name badges, then store that information in the table. Make up a mail merge document that uses that table as the datasource and will print the document out as you like. Next, record a word macro that follows your steps through the population of the fields and the printout of the merged document. Save all of this and then use the following code to open your document and print it out: <cftry> <cfobject action="CONNECT" class="Word.Application" name="objWord" type="COM"> <cfcatch> <cfobject action="CREATE" class="Word.Application" name="objWord" type="COM"> </cfcatch> </cftry> <cftry> <cfscript> objDoc = objWord.Documents; newDoc = objDoc.open("C:\inetpub\web_documents\mergedocument.doc"); switch (form.printer) { case 1: {objWord.ActivePrinter = "HP LaserJet 1200 Series PCL"; break;} case 2: {objWord.ActivePrinter = "\\lana\HP LaserJet 4050 Series PCL"; break;}} objWord.Run("Macroname"); newDoc.Close(); </cfscript> <cfcatch> <cfscript> // newDoc.Close(); objWord.Quit(); </cfscript> <cfrethrow> </cfcatch> </cftry> It takes a bit of work but it does work well. Write back if you have any questions. - Matt Small -----Original Message----- From: Helge Hetland [mailto:[EMAIL PROTECTED]] Sent: Friday, February 01, 2002 10:19 AM To: Matthew R. Small Subject: RE: Help - MS Word outputfrom CF? Hello! I would be very interested in some example code showing this. I'm investigating ways to produce nametags (badges) directly to Word->printer from CF. Thanks, Helge Hetland WebSite AS Norway -----Original Message----- From: Matthew R. Small [mailto:[EMAIL PROTECTED]] Sent: 1. februar 2002 15:58 To: CF-Talk Subject: RE: Help - MS Word outputfrom CF? Hi Ed, I do a lot of this, and I think in the easiest way: I generate an HTML page and store the entire thing in a variable, then write it to a file. I then open the file up in Word via CFOBJECT and have Word print it out automatically. It takes some work to get it set up properly but it's worth it when those sheets start feeding out of the printer. I can give you some code to follow if you're interested. - Matt Small -----Original Message----- From: Ed Gordon [mailto:[EMAIL PROTECTED]] Sent: Friday, February 01, 2002 12:30 AM To: CF-Talk Subject: Help - MS Word outputfrom CF? After the users put in parameters to generate a "quote" and the customer says 'yes', I need to generate a whole big document, including standard paragraphs, and other sections of text inserted based on various aspects of the sale. This is now all done by hand in MS Word. I can get all the parts (text paragraphs), but how do I then have CF spit out a nicely-formatted text document? In general, what DO people do about printed reports???!?!?! I have so far only put a printer-friendly web page out and the user prints it in his browser. This is supposed to be a nicely formatted printed document, an actual legal contract for the customer to sign. Anyone do this type of thing before? Thanks! Ed Gordon CF Developer [EMAIL PROTECTED] ______________________________________________________________________ Get Your Own Dedicated Windows 2000 Server PIII 800 / 256 MB RAM / 40 GB HD / 20 GB MO/XFER Instant Activation · $99/Month · Free Setup http://www.pennyhost.com/redirect.cfm?adcode=coldfusionb FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists