Adobe ColdFusion 8

Example 2: Using the Microsoft Word application object

The following example uses the Microsoft Word application COM object in the Application scope to convert a Word document to HTML. This example works with Word 2000 as written. To work with Word 97, change "Val(8)" to "Val(10)".

This example uses an Application scope lock to ensure that no other page interrupts creating the object. Once the Word object exists, the example uses a named lock to prevent simultaneous access to the file that is being converted.

<cfapplication name="comtest" clientmanagement="No" Sessionmanagement="yes">
<!--- Uncomment the following line if you need to delete the object from the
Application scope --->
<!--- <cfset structdelete(Application, "MyWordObj")> --->

<!--- use the GetTickCount function to get a current time indicator, used for
        displaying the total processing time. --->
<cfset start = GetTickCount()>
<!--- If necessary, create the Word.application object and put it in the
        Application scope --->
<cfset WordObj_is_initialized = False>
<cflock scope="application" type="readonly" timeout=120>
    <cfset WordObj_is_initialized = StructKeyExists(application, "MyWordObj")>
</cflock>
<cfif not WordObj_is_initialized >
    <cflock scope="Application" type="exclusive" timeout="120">
        <cfif not StructKeyExists(application, "MyWordObj")>

<!--- First try to connect to an existing Word object --->
            <cftry>
                <cfobject type="com"
                    action="connect"
                    class="Word.application"
                    name="Application.MyWordobj"
                    context="local"> 
                <cfcatch>
<!--- There is no existing object, create one --->
                    <cfobject type="com"
                        action="Create"
                        class="Word.application"
                        name="Application.MyWordobj"
                        context="local"> 
                </cfcatch>
            </cftry>
            
            <cfset Application.mywordobj.visible = False>
        </cfif>
    </cflock>
</cfif>

<!--- Convert a Word document in temp.doc to an HTML file in temp.htm. --->
<!--- Because this example uses a fixed filename, multiple pages might try
    to use the file simultaneously. The lock ensures that all actions from
    reading the input file through closing the output file are a single "atomic"
    operation, and the next page cannot access the file until the current page
    completes all processing. 
    Use a named lock instead of the Application scope lock to reduce lock contention. --->
<cflock name="WordObjLock" type="exclusive" timeout="120">
    <cfset docs = application.mywordobj.documents()>
    <cfset docs.open("c:\CFusion\wwwroot\temp.doc")>
    <cfset converteddoc = application.mywordobj.activedocument>
    <!--- Val(8) works with Word 2000. Use Val(10) for Word 97 --->
    <cfset converteddoc.saveas("c:\CFusion\wwwroot\temp.htm",val(8))>
    <cfset converteddoc.close()>
</cflock>

<cfoutput>
    Conversion of temp.htm Complete<br>
    Execution Time: #int(getTickCount()-start)# milliseconds<br>
</cfoutput>