Adobe ColdFusion 8

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 vars --->
    <cfset var pingClass="">
    <cfset var pingReply="">
    <!--- Get Ping class --->
    <cfobject type=".NET" name="pingClass"
            class="System.Net.NetworkInformation.Ping">
    <!--- Perform synchronous ping (using defaults) ---> 
    <cfset pingReply=pingClass.Send(Arguments.host)>
    <!--- Return result --->
    <cfreturn pingReply.Get_Status().ToString()>
</cffunction>

<h3>Ping Test</h3>
<cfoutput>
    127.0.0.1: #Ping("127.0.0.1")#<br>
    www.adobe.com: #Ping("www.adobe.com")#<br>
</cfoutput>

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, you must compile this class and the Microsoft Interop.Word.dll file, and place them in the C:\dotnet directory. (Alternatively, you can put them elsewhere and change the paths in the cfobject assembly attribute.) You might 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#">
</cfif>
<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 should start a
            a new line.")>
    <cfset wordCreator.newParagraph()>
    <cfset wordCreator.newParagraph()>
</cfloop>
<cfset wordCreator.save()>

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 may 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 = "normal.dot";
        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;
            }
            doc.Activate();
        }

        public void addText(String text) {
            app.Selection.TypeText(text);
        }

        public void newParagraph() {
            app.Selection.TypeParagraph();
        }

        public void save() {
            if(!isNewDoc)
                doc.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);
        }
    } 
}