The first application example uses a Microsoft .NET system class method directly. The second application example uses a custom C# class to access Microsoft Word.

Example: Using a .NET class directly

The following example uses the Microsoft .NET System.Net.NetworkInformation.Ping class method directly to ping servers. This class is supported in .NET version 2.0 and later.

<!--- This function pings the specified host. --->
<cffunction name="Ping" returntype="string" output="false">
<cfargument name="host" type="string" required="yes">
<!--- Local variables --->
<cfset var pingClass="">
<cfset var pingReply="">
<!--- Get Ping class --->
<cfobject type=".NET" name="pingClass"
<!--- Perform synchronous ping (using defaults) --->
<cfset pingReply=pingClass.Send(>
<!--- Return result --->
<cfreturn pingReply.Get_Status().ToString()>

<h3>Ping Test</h3>
<cfoutput> #Ping("")#<br> #Ping("")#<br>

Example: Using a custom class to access Microsoft Word

The following ColdFusion application uses a custom C# WordCreator class, and supporting classes in Microsoft Office and Word DLLs, to create a Word document. The application opens Microsoft Word, writes five copies of the text specified by the someText variable, and saves the document in the file specified by the filename variable. The application leaves the instance of Word open.

Note: For an example that uses a .NET System class directly and does not require any cousin .NET code, see the "Limitations" section of [cfobject: .NET object] in the CFML Reference.

The second listing shows the WordCreator C# source code. To run this application locally, compile this class and the Microsoft Interop.Word.dll file, and place them in the C:\dotnet directory. (Alternatively, you can place them elsewhere and change the paths in the cfobject assembly attribute.) You could need additional or different Microsoft DLL files, depending on the version of Microsoft Office that you have installed.
The ColdFusion application contains the following code:

<cfset filename="C:\dotNet\demo.doc">
<cfif fileexists(filename)>
<cffile action="delete" file="#filename#">
<cfobject type=".NET" assembly="C:\dotNetApp\WordApp.dll,C:\dotNet\Interop.Office.dll" name="wordCreator" class="WordApp.WordCreator">
<cfset wordCreator.init("#filename#")>
<cfdump label="WordCreator Class Dump" var="#wordCreator#">

<cfset someText = "ColdFusion created this sample document using Windows .NET class methods. The text is long enough to appear in the Word file on multiple lines.">

<cfloop from=1 to=5 index =i>
<cfset wordCreator.addText(someText)>
<cfset wordCreator.newParagraph()>
<cfset wordCreator.newParagraph()>
<cfset wordCreator.addText("Starting a new paragraph. It starts a
a new line.")>
<cfset wordCreator.newParagraph()>
<cfset wordCreator.newParagraph()>

The C# source for the WordCreator class is as follows:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

// The commented-out lines could be needed on some systems in place of,
// or in addition to, the line that follows them.
// using Microsoft.Office.Core;
// using Word;
// using Microsoft.Office;
// using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
namespace WordApp {
public class WordCreator {
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
object docType = 0;
object newTemplate = false;
object template = "";
object format = WdSaveFormat.wdFormatDocument;
ApplicationClass app = new ApplicationClass();
private object fileName;
private Document doc;
private bool isNewDoc = false;

public WordCreator(String fileName) {
this.fileName = fileName;
app.Visible = true;
if (File.Exists(fileName))
doc = app.Documents.Open(ref this.fileName, ref missing, ref
readOnly, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref
isVisible, ref missing, ref missing, ref missing, ref missing);
else {
doc = app.Documents.Add(ref template, ref newTemplate,
ref docType, ref isVisible);
isNewDoc = true;

public void addText(String text) {

public void newParagraph() {

public void save() {
else doc.SaveAs(ref fileName, ref format, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy