Windows Systems Programming: Spring 2002

[ Home | Syllabus | Course Notes | Assignments | Search]


Motivating the Component Object Model: Examples

Before we get too mired in the details of COM programming. let's examine some examples which illustrate the advantages of utilizing COM.

The entire office suite of Microsoft is written using COM. Therefore, various pieces of EXCEL. WORD, ACCESS, etc can be used in your program by accessing its COM interface.

Here are some examples of what you can do easily with the COM interfaces into OFFICE Suite.

Accessing COM objects

// Windows Script Host Sample Script
//
// ------------------------------------------------------------------------
//               Copyright (C) 1996 Microsoft Corporation
//
// You have a royalty-free right to use, modify, reproduce and distribute
// the Sample Application Files (and/or any modified version) in any way
// you find useful, provided that you agree that Microsoft has no warranty,
// obligations or liability for any Sample Application Files.
// ------------------------------------------------------------------------

// This sample will display Windows Scripting Host properties in Excel.


var vbOKCancel = 1;
var vbInformation = 64;
var vbCancel = 2;

var L_Welcome_MsgBox_Message_Text    = "This script will display Windows Scripting Host properties in Excel.";
var L_Welcome_MsgBox_Title_Text      = "Windows Scripting Host Sample";
Welcome();
    

//////////////////////////////////////////////////////////////////////////////////
//
// Excel Sample
//
var objXL = WScript.CreateObject("Excel.Application");

objXL.Visible = true;

objXL.WorkBooks.Add;

objXL.Columns(1).ColumnWidth = 20;
objXL.Columns(2).ColumnWidth = 30;
objXL.Columns(3).ColumnWidth = 40;

objXL.Cells(1, 1).Value = "Property Name";
objXL.Cells(1, 2).Value = "Value";
objXL.Cells(1, 3).Value = "Description";

objXL.Range("A1:C1").Select; hptrd_left.gif (955 bytes)
objXL.Selection.Font.Bold = true;
objXL.Selection.Interior.ColorIndex = 1;
objXL.Selection.Interior.Pattern = 1; //xlSolid
objXL.Selection.Font.ColorIndex = 2;

objXL.Columns("B:B").Select;
objXL.Selection.HorizontalAlignment = -4131; // xlLeft

var intIndex = 2;

function Show(strName, strValue, strDesc) {
    objXL.Cells(intIndex, 1).Value = strName;
    objXL.Cells(intIndex, 2).Value = strValue;
    objXL.Cells(intIndex, 3).Value = strDesc;
    intIndex++; hptrd_left.gif (955 bytes)
    objXL.Cells(intIndex, 1).Select;
}

//
// Show WScript properties
//
Show("Name",           WScript.Name,           "Application Friendly Name");
Show("Version",        WScript.Version,        "Application Version");
Show("FullName",       WScript.FullName,       "Application Context: Fully Qualified Name");
Show("Path",           WScript.Path,           "Application Context: Path Only");
Show("Interactive",    WScript.Interactive,    "State of Interactive Mode");


//
// Show command line arguments.
//
var colArgs = WScript.Arguments
Show("Arguments.Count", colArgs.length, "Number of command line arguments");

for (i = 0; i < colArgs.length; i++) {
    objXL.Cells(intIndex, 1).Value = "Arguments(" + i + ")";
    objXL.Cells(intIndex, 2).Value = colArgs(i);
    intIndex++;
    objXL.Cells(intIndex, 1).Select;
}


//////////////////////////////////////////////////////////////////////////////////
//
// Welcome
//
function Welcome() {
    var WSHShell = WScript.CreateObject("WScript.Shell");
    var intDoIt;

    intDoIt =  WSHShell.Popup(L_Welcome_MsgBox_Message_Text,
                              0,
                              L_Welcome_MsgBox_Title_Text,
                              vbOKCancel + vbInformation );
    if (intDoIt == vbCancel) {
        WScript.Quit();
    }
}
rainbow.gif (2243 bytes)

 

// Windows Script Host Sample Script (In JScript)
//
// ------------------------------------------------------------------------
//               Copyright (C) 1996-1997 Microsoft Corporation
//
// You have a royalty-free right to use, modify, reproduce and distribute
// the Sample Application Files (and/or any modified version) in any way
// you find useful, provided that you agree that Microsoft has no warranty,
// obligations or liability for any Sample Application Files.
// ------------------------------------------------------------------------


// This sample demonstrates how to access Microsoft Excel using the Windows Scripting Host.

var vbOKCancel = 1;
var vbInformation = 64;
var vbCancel = 2;

var L_Welcome_MsgBox_Message_Text    = "This script demonstrates how to access Excel using the Windows Scripting Host.";
var L_Welcome_MsgBox_Title_Text      = "Windows Scripting Host Sample";
Welcome();
    
//////////////////////////////////////////////////////////////////////////////////
//
// Excel Sample
//

var objXL;

objXL = WScript.CreateObject("Excel.Application");
objXL.Workbooks.Add;
objXL.Cells(1,1).Value = 5;
objXL.Cells(1,2).Value = 10;
objXL.Cells(1,3).Value = 15
objXL.Range("A1:C1").Select;

var objXLchart = objXL.Charts.Add();
objXL.Visible = true;
objXLchart.Type = -4100;

var intRotate;
for(intRotate = 5; intRotate <= 180; intRotate += 5) {
    objXLchart.Rotation = intRotate;
}

for (intRotate = 175; intRotate >= 0; intRotate -= 5) {
    objXLchart.Rotation = intRotate;
}

//////////////////////////////////////////////////////////////////////////////////
//
// Welcome
//
function Welcome() {
    var WSHShell = WScript.CreateObject("WScript.Shell");
    var intDoIt;

    intDoIt =  WSHShell.Popup(L_Welcome_MsgBox_Message_Text,
                              0,
                              L_Welcome_MsgBox_Title_Text,
                              vbOKCancel + vbInformation );
    if (intDoIt == vbCancel) {
        WScript.Quit();
    }
}

To demonstrate language independence - here is the same program in Visual Basic Script

// Windows Script Host Sample Script (In Visual Basic Script)

' Windows Script Host Sample Script
'
' ------------------------------------------------------------------------
'               Copyright (C) 1996-1997 Microsoft Corporation
'
' You have a royalty-free right to use, modify, reproduce and distribute
' the Sample Application Files (and/or any modified version) in any way
' you find useful, provided that you agree that Microsoft has no warranty,
' obligations or liability for any Sample Application Files.
' ------------------------------------------------------------------------

' This sample demonstrates how to access Microsoft Excel using the Windows Scripting Host.

L_Welcome_MsgBox_Message_Text    = "This script demonstrates how to access Excel using the Windows Scripting Host."
L_Welcome_MsgBox_Title_Text      = "Windows Scripting Host Sample"
Call Welcome()
    
' ********************************************************************************
' *
' * Excel Sample
' *

Dim objXL
Dim objXLchart
Dim intRotate

Set objXL = WScript.CreateObject("Excel.Application")
objXL.Workbooks.Add
objXL.Cells(1,1).Value = 5
objXL.Cells(1,2).Value = 10
objXL.Cells(1,3).Value = 15
objXL.Range("A1:C1").Select

Set objXLchart = objXL.Charts.Add()
objXL.Visible = True
objXLchart.Type = -4100     

For intRotate = 5 To 180 Step 5
    objXLchart.Rotation = intRotate
Next

For intRotate = 175 To 0 Step -5
    objXLchart.Rotation = intRotate
Next

' ********************************************************************************
' *
' * Welcome
' *
Sub Welcome()
    Dim intDoIt

    intDoIt =  MsgBox(L_Welcome_MsgBox_Message_Text, _
                      vbOKCancel + vbInformation,    _
                      L_Welcome_MsgBox_Title_Text )
    If intDoIt = vbCancel Then
        WScript.Quit
    End If
End Sub


Copyright chris wild 1999-2002.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 17, 2002.