User Guide

Table Of Contents
Creating and using COM objects 953
<!--- Second, use the form variables from the user entry form to populate a
number
of properties necessary to create and send the message. --->
<cfset Mailer.From = "#Form.fromName#">
<cfset Mailer.To = "#Form.to#">
<cfset Mailer.Subject = "#Form.subject#">
<cfset Mailer.Importance = 2>
<cfset Mailer.Body = "#Form.body#">
<cfset Mailer.Cc = "#Form.cc#">
<!--- Last, use the Send() method to send the message.
Invoking the Send() method destroys the object.--->
<cfset Mailer.Send()>
Note: Use the
cftry and cfcatch tags to handle exceptions thrown by COM objects. For more
information on exception handling, see Chapter 14, “Handling runtime exceptions with ColdFusion
tags,” on page 322.
Releasing COM objects
By default, COM object resources are released when the Java garbage collector cleans them. You
can use the
ReleaseCOMObject function to immediately release resources if an object is no longer
needed.
Use the
ReleaseCOMObject function to release COM objects that are launched as an external
process, such as Microsoft Excel. The garbage collector might not clean these processes in a short
time, resulting in multiple external processes running, which drains system resources.
If the COM object has an end method, such as a quit method that terminates the program, call
this method before you call the
ReleaseComObject function. If you use the ReleaseComObject
function on an object that is in use, the object is prematurely released and your application will
get exceptions.
Example
The following example creates a Microsoft Excel application object, uses it, then releases the
object when it is no longer needed:
<h3>ReleaseComObject Example</h3>
<cfscript>
obj = CreateObject("Com", "excel.application.9");
//code that uses the object goes here
obj.quit();
ReleaseComObject(obj);
</cfscript>
General COM object considerations
When you use COM objects, consider the following to prevent and resolve errors:
Ensuring correct threading
Using input and output arguments
Understanding common COM-related error messages
The following sections describe these issues.