Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / March 2007



Tip: Looking for answers? Try searching our database.

HTML2Word.cfm custom tag

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HandersonVA - 29 Mar 2007 18:29 GMT
have you used this custom tab? I downloaded this from coldfusion support and
wonder it is working on windows 2003. When I downloaded this file, windows 2003
did not even come out and comments on this file said "This tag will only work
on WindowsNT/2000". however shouldn't be working on windows 2003 operating
system? can you help? (I use CFMX 6.1 update)

<!---
!!! CREATE DYNAMIC MS WORD FILES WITH CUSTOM FORMATTING: TABLES, COLORS,
BORDERS, FONTS ETC.!!!
This custom tag creates a dynamic  Word file from the generated content of a
block of ColdFusion code.
The generated content is first saved as an HTML file,
Then it is opened in Word and saved with the .doc extension.

!!!--------
Do you need to generate colorful MS Excel documents on the fly?
Check out CF_HTML2Excel in the Allaire Tag gallery.
!!!--------

System Requirements:
- This tag will only work on WindowsNT/2000
- MS Word or OSE (Office Server Extensions/ requires IIS) MUST BE INSTALLED on
the server
- Office HTML filters must also be installed (try to open a simple HTML file
manually in Word - on the server - to check this. You should NOT see the HTML
source.)
- ColdFusion must have permissions to access/use Word COM objects
(Download OLEView from www.microsoft.com and check the launch and access
permissions of the "Microsoft Word Application" COM object)
 
Usage:
<CF_HTML2Word
    DIRECTORY="C:\InetPub\wwwroot\"
    TEMPDIRECTORY="C:\Temp\"
    FILENAME="myWordFile"
    LEAVEOPEN="YES/NO">
 
Any static or dynamically generated HTML goes here

</CF_HTML2Word>

Input Parameters:
DIRECTORY(optional): the directory path where the Word file will be created;
default is the current template directory
TEMPDIRECTORY(optional): the directory path where the temporary HTML files are
stored: default is the current template directory
FILENAME(optional): the name of the Word file that will be created (without
the extension); default is a random number preceded by HTML2Word(eg.
HTML2Word5545678.doc)
LEAVEOPEN(optional/use carefully): leaves the Word application open in the
background after it finishes one request. Use this option if you expect
multiple requests in a short period of time. It can save Word start-up time but
it uses server resources.

Output Parameters:
WORDFILE : The absolute path to the just-created Word File

Note: If another Word file with same name exists the tag will overwrite it
without prompting!!

Note about creating Word documents with embedded images:  
It is posible to create such Word documents in one of two ways:
    - resolve all img src paths like so <IMG
SRC="http://www.myserver.com/myimage.gif">(the images must reside on the web
server..)
    - make all image paths relative to a 'images' directory - similar to what
MS IE does when you save an HTML page as 'Web Page Complete'
The bottom line is: if you can manually open the HTML file in Word and see the
images you can also do it in CF through scripting
End Note

--->

<CFPARAM NAME="Attributes.Directory"
DEFAULT="#GetDirectoryFromPath(GetTemplatePath())#">
<CFPARAM NAME="Attributes.TempDirectory"
DEFAULT="#GetDirectoryFromPath(GetTemplatePath())#">
<CFPARAM NAME="Attributes.FileName"
DEFAULT="HTML2Word#RandRange(1000,100000000)#">
<CFPARAM NAME="Attributes.LeaveOpen" DEFAULT="No">

<CFIF ThisTag.HasEndTag>
    <CFSWITCH EXPRESSION="#ThisTag.ExecutionMode#">
   
        <CFCASE VALUE="START">
            <!---  HTML file name is random --->
            <CFSET HTMLFileName = "HTML2Word" & RandRange(1000,100000000)>
            <CFSET HTMLFilePath = Attributes.TempDirectory & HTMLFileName  &
".html">
            <CFSET WordFilePath = Attributes.Directory & Attributes.FileName &
".doc">
           
            <!--- Delete any files that have the same names --->
            <CFIF FileExists(HTMLFilePath)>
                <CFTRY>
                    <CFFILE ACTION="DELETE" FILE="#HTMLFilePath#">
                <CFCATCH TYPE="Any">            
                    <CFABORT SHOWERROR="<FONT COLOR='RED'>Error
Occured!!<BR>Cannot delete file <B>#HTMLFilePath#.</B><BR>It may be locked by
Word. Close Word and try again.<BR></FONT><B>Error Details:</B>  
#CFCATCH.MESSAGE#">
                </CFCATCH>
                </CFTRY>
            </CFIF>
           
            <CFIF FileExists(WordFilePath)>
                <CFTRY>
                    <CFFILE ACTION="DELETE" FILE="#WordFilePath#">
                <CFCATCH TYPE="Any">
                   
                </CFCATCH>
                </CFTRY>
            </CFIF>
            <!--- End file delete --->
        </CFCASE>
       
        <CFCASE VALUE="END">
            <!--- write the HTML file to disk (contains the generated content)
--->
            <CFFILE ACTION="WRITE" FILE="#HTMLFilePath#"
OUTPUT="#ThisTag.GeneratedContent#">
           
            <!--- discard the generated content so it does not show on the
page --->
            <CFSET ThisTag.GeneratedContent = "">
                               
            <!--- try to connect to Word --->
            <CFTRY>
                <CFOBJECT
                    ACTION="CONNECT"
                    CLASS="Word.Application"
                    NAME="objWord"
                    TYPE="COM">
              <CFCATCH>
                    <CFTRY>
                        <CFOBJECT
                            ACTION="CREATE"
                            CLASS="Word.Application"
                            NAME="objWord"
                            TYPE="COM">
                       
                    <CFCATCH TYPE="ANY">
                            <CFABORT SHOWERROR="<FONT COLOR='RED'>Cannot
create Word Object<BR>Make sure Word is installed and that ColdFusion has
permissions to use the Word COM objects</FONT><BR><B>Error Details:</B>  
#CFCATCH.MESSAGE#">                    
                        </CFCATCH>
                    </CFTRY>
              </CFCATCH>
            </CFTRY>
   
        <CFTRY>
            <CFSCRIPT>
                // open Word in the background
                objWord.Visible = false;  
               
                 // disable Alerts such as: 'Save this document?'
                objWord.DisplayAlerts =false;
               
                // get the 'Documents' collection
                objDoc = objWord.Documents;
                 
                //open the HTML document
                newDoc = objDoc.open(HTMLFilePath);
               
               
                // save it as a new document
                newDoc.SaveAs(WordFilePath,Val(1));
               
                // close the document
                newDoc.Close();
               
                if(Attributes.LeaveOpen IS "No"){
                    // quit Word
                    objWord.Quit();
                }
               
                //release the object
                objWord = "Nothing";
            </CFSCRIPT>
           
            <CFCATCH TYPE="ANY">
                <CFSCRIPT>
                    objWord.Quit();
                </CFSCRIPT>
                <CFABORT SHOWERROR="<FONT COLOR='RED'>Error occured while
connected to the Word object!</FONT><BR>Error Details:  #CFCATCH.MESSAGE#">
            </CFCATCH>
        </CFTRY>  
           
            <!--- delete intermediary HTML file --->
            <CFTRY>
                <CFFILE ACTION="DELETE" FILE="#HTMLFilePath#">
                <CFCATCH TYPE="Any">
                </CFCATCH>
            </CFTRY>
           
            <!--- pass the generated file name to the calling template--->
            <CFSET Caller.WordFile = WordFilePath>
                           
        </CFCASE>
    </CFSWITCH>
<CFELSE>
    <PRE>
    Error: This tag requires an end tag!!
    Usage:
    <FONT COLOR="MAROON">&lt;CF_HTML2WORD&gt;</FONT>
    Dynamically generated HTML to convert to MS Word format
    <FONT COLOR="MAROON">&lt;/CF_HTML2WORD</FONT>
    <BR>
    Problems? Contact the author at:&nbsp;<A
HREF="mailto:test@test.com?Subject=cf_html2word">test@test.com</A>
    </PRE>
</CFIF>
insuractive - 29 Mar 2007 20:18 GMT
the only part of the document that might possibly cause a problem is the class
name the <Cfobject> tag is using to access MS word:  Word.Application.  As long
as you have word installed on your server and it uses the same class name* it
should work fine.

* not exactly sure how to figure out what class name to use, though.  Maybe
try the following and see if you get an error:

<CFTRY>
                <CFOBJECT
                    ACTION="CONNECT"
                    CLASS="Word.Application"
                    NAME="objWord"
                    TYPE="COM">
              <CFCATCH>
                    <CFTRY>
                        <CFOBJECT
                            ACTION="CREATE"
                            CLASS="Word.Application"
                            NAME="objWord"
                            TYPE="COM">
                       
                    <CFCATCH TYPE="ANY">
                            <CFABORT SHOWERROR="<FONT COLOR='RED'>Cannot
create Word Object<BR>Make sure Word is installed and that ColdFusion has
permissions to use the Word COM objects</FONT><BR>[B]Error Details:[/B]  
#CFCATCH.MESSAGE#">                    
                        </CFCATCH>
                    </CFTRY>
              </CFCATCH>
            </CFTRY>
HandersonVA - 30 Mar 2007 12:54 GMT
I tried and still got the same error message as below. (btw, the code that you
provided is same as that I posted here)
something went wrong with connection to the word object on win2003.
btw, any other custom tag to convert HTML to Word other than I posted? thank
you.
--------------------------------------------------------
Error Occurred While Processing Request  
<FONT COLOR=&apos;RED&apos;>Error occured while connected to the Word
object!</FONT><BR>Error Details: An exception occurred when executing a Com
method.  
 
 
The error occurred in E:\CFusionMX\CustomTags\HTML2Word.cfm: line 149
 
147 :                     objWord.Quit();
148 :                 </CFSCRIPT>
149 :                 <CFABORT SHOWERROR="<FONT COLOR='RED'>Error occured
while connected to the Word object!</FONT><BR>Error Details:  
#CFCATCH.MESSAGE#">
150 :             </CFCATCH>
151 :         </CFTRY>  


--------------------------------------------------------------------------------
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.