Windows NT Systems Programming: Spring 2000

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


Script COM objects

<?xml version="1.0"?>

<component>
   <registration description="FileFinder"
                 progid="FileFinder.WSC"
                 version="1.00"
                 classid="{82e84cc0-23f8-11d3-88e4-00105a68bf5d}">
   </registration>

   <public>
      <property name="FileSpec">
         <get/>
         <put/>
      </property>

      <property name="Date">
         <get/>
         <put/>
      </property>

      <method name="Search">
      </method>

      <event name="FileFound">
         <parameter name="fileName" />
      </event>
   </public>

   <script language="VBScript">
      <![CDATA[ hptrd_left.gif (955 bytes)
         dim FileSpec
         FileSpec = "c:\"

         dim Date
         Date = Now

         function get_FileSpec()
	    get_FileSpec = FileSpec
         end function

         function put_FileSpec(newValue)
            set fso = CreateObject("Scripting.FileSystemObject")
            if fso.FolderExists(newValue) then
               FileSpec = newValue
            end if
         end function

         function get_Date()
            get_Date = Date
         end function

         function put_Date(newValue)
            if IsDate(newValue) then
               Date = newValue
            end if
         end function

         function Search()
            set fso = CreateObject("Scripting.FileSystemObject")
            set fld = fso.GetFolder(FileSpec)
            set files = fld.Files

            set dic = CreateObject("Scripting.Dictionary")
            for each f in files
               d = DateDiff("d", f.DateLastModified, Date)
               if d = 0 then
                  FireEvent "FileFound", f.Name
                  dic.add f.Name, f.Name
               end if
            next

            set Search = dic
         end function
      ]]>
   </script>
</component>
rainbow.gif (2243 bytes)

Using a the FILEFINDER.WSC

' FFX.vbs
' Demonstrates the use of the FileFinder WSC component
' Usage: FFX [path] [date]
' ---------------------------------------------------------

Dim oFF, str
Set oFF = WScript.CreateObject("FileFinder.WSC", "FileFinder_")

if WScript.Arguments.Count > 0 then
   if WScript.Arguments.Item(0) = "/?" then hptrd_left.gif (955 bytes)
       WScript.Echo "Usage: FF [path] [date]"
       WScript.Quit
   end if
end if

if WScript.Arguments.Count = 2 then
   oFF.FileSpec = WScript.Arguments.Item(0)
   oFF.Date = WScript.Arguments.Item(1)
else
   if WScript.Arguments.Count = 1 then
      oFF.FileSpec = WScript.Arguments.Item(0)
      oFF.Date = Now hptrd_left.gif (955 bytes)
   else
      oFF.FileSpec = InputBox("Enter the folder name:",, "c:\")hptrd_left.gif (955 bytes)
      if oFF.FileSpec <> "" then
         oFF.Date = InputBox("Enter the date as a string:",, Date)
      else
         WScript.Echo("You must provide the name of an existing folder.")
         WScript.Quit
      end if
   end if
end if

oFF.Search()

if str = "" then 
   str = "No file found."
end if

WScript.Echo str

' -----------------------------------------------------
' Event handler
' -----------------------------------------------------
sub FileFinder_FileFound(fileName) hptrd_left.gif (955 bytes)
   str = str & fileName & vbCrLf
end sub
rainbow.gif (2243 bytes)

Creating a Type Library


Dialog Boxes

You could put a COM wrapper around any C++ dialog (like ex25b.auto)

But learning C++ just to create a way to get input is a complex way


 

DHTML

Property Description
Document reference to the DHTML object
Methods  
Create Creates dialog from HTML page (does not display)
Show Displays the dialog
Move Moves position of dialog
GetItemText/SetItemText read/wirte innerHTML property of tag
GetItemValue/SetItemValue Read/Write value property of tag
GetItemSrc/SetItemSrc Read/Write Source property of tag
GetItemHref/SetItemHref Read/Write href property of tag
  • Example which demostrates
    • jscript calculation at the browser (before submitting) to compute totals and amount payable
    • Setting of fields when starting up (like data exchange into dialog)
    • Access of fields when submitted (like data exchange on dialog done)

New Invoice #  on date:
The Company
Address and number
ZIP Code City
Country

 
To:


Quantity

Description

Price

Total

  USD   UKP

Thank you!
Visit us at: Company Web Site


Here is the HTML for the above

<html>
<head>
<title>New Invoice</title>
</head>
<script>
//// embed jscript in html page
USDLIRE = 1760;
UKPLIRE = 2870;
function Price_OnKeyPress()
//// called when a key is pressed (see call below)
{
    if(window.event.keyCode == 13)
//// check for "enter" key
    {
        total.innerHTML = quantity.value * price.value;
//// access the value propeties of the price and quantity tags (see below)
        payable.innerHTML = total.innerHTML;
        if(usd.checked)
//// check which currency is being used
            lire.innerHTML = total.innerHTML * USDLIRE + " dollars"
//// convert to Italian lire
        else
            lire.innerHTML = total.innerHTML * UKPLIRE + " lire"
    }
}
</script>
 
<
body bgproperties="fixed" bgcolor="#D6EFC7">
<
table width="100%" bgcolor="#EDDDD1">
<tr>
<
td width="70%"><font face="Verdana"><strong><font color="#000000"><small>New Invoice</small>
#</font>&nbsp; </strong><
input type="text" tip="Unique numeric invoice #" id="invoiceID"
size="
10" maxlength="4"></font></td>
<
td width="30%" align="right"><font face="Verdana"><font color="#000000"><strong><small>on
date: </small></strong></font><
input type="text" tip="Date of the invoice (dd/mm/yyyy)"
name="
invoiceDate" size="15"></font></td>
</tr>
</table>
<
table border="0" width="100%" bgcolor="#FFF5E8">
<tr>
<
td tip="Invoicing Company" width="50%"><img id="companylogo" src="Windows.gif" width="32"
height="
32" align="absmiddle"><font face="Verdana"><strong><big id="CompanyName"> The
Company</big><br>
<small><
span id="CompanyAddress">Address and number</span><br>
<
span id="CompanyCity">ZIP Code&nbsp;City</span><br>
<
span id="CompanyCountry">Country</span></small></strong><br>
<em><small><
span id="CompanyPhone">&nbsp;</span></small></em></font></td>
<
td width="50%"><font face="Verdana"><strong><big><big>To:</big></big> </strong><textarea
rows="4" id="recipient" tip="Recipient of the invoice" name="recipient" cols="43"></textarea></font></td>
</tr>
</table>
<p><br>
</p>
<
table border="1" width="100%" bgcolor="#F3F3F3">
<tr>
<
td width="12%" bgcolor="#C0C0C0"><p align="center"><small><font face="Verdana"><strong>Quantity</strong></font></small></td>
<
td width="48%" bgcolor="#C0C0C0"><p align="center"><small><font face="Verdana"><strong>Description</strong></font></small></td>
<
td width="20%" bgcolor="#C0C0C0"><p align="center"><small><font face="Verdana"><strong>Price</strong></font></small></td>
<
td width="20%" bgcolor="#C0C0C0"><p align="center"><small><font face="Verdana"><strong>Total</strong></font></small></td>
</tr>
<tr>
<
td width="12%" valign="top"><p align="center"><font face="Verdana"><input type="text"
tip="
Quantity of the items" id="quantity" size="6"></font></td> hptrd_left.gif (955 bytes) //// this is the "quantity" field
<
td width="50%" valign="top"><p align="left"><font face="Verdana"><textarea rows="3"
tip="
Description of the item" name="description" cols="43"></textarea></font></td>
<
td width="19%" valign="top"><p align="center"><font face="Verdana"><input type="text"
tip="
Price of the single item" id="price" size="15" onkeypress="Price_OnKeyPress()"></font></td>  hptrd_left.gif (955 bytes) //// this is the "price" field
<
td width="19%" align="right" valign="top"><span tip="Total amount for the items"
id="
total"></span></td>   hptrd_left.gif (955 bytes) //// this is the "total"  field
</tr>
</table>
<
table border="1" width="100%" height="26">
<tr>
<
td width="62%" height="20"><span id="lire"
tip="
The amount payable expressed in Italian lire"></span></td>
<
td width="19%" height="20">&nbsp;<small><strong><font face="Verdana"> <input type="radio"
id="
usd" tip="if checked the invoice currency is USD" checked name="R1">USD&nbsp;&nbsp; <input
type="radio" id="ukp" tip="if checked the invoice currency is UKP" name="R1">UKP</font></strong></small></td>

<td width="19%" align="right" height="20"><span tip="Total amount payable" id="payable"></span></td>  hptrd_left.gif (955 bytes) //// this is the "payable" field
</tr>
</table>
<
p id="notes"><strong>Thank you!<br>
</strong>Visit us at: <
a id="companysite">Company Web Site</a> </p>
</body>
</html>

 


' Invoice.vbs
' Demonstrates HTML-based dialog boxes in VBScript
' ------------------------------------------------

Dim dlg
Set dlg = CreateObject("WshKit.DialogBox")hptrd_left.gif (955 bytes)
If dlg.Create("path://invoice.htm") = True Then hptrd_left.gif (955 bytes)
   dlg.SetItemSrc "companylogo", "expoware.gif" hptrd_left.gif (955 bytes)
   dlg.SetItemText "companyname", "Expoware Soft"
   dlg.SetItemText "companyaddress", "One Expoware Way, 1"
   dlg.SetItemText "companycity", "00000 Roundmond"
   dlg.SetItemText "companycountry", "Rome, Italy"
   dlg.SetItemText "companyphone", "+39 06 12345678"
   dlg.SetItemValue "invoiceID", "7" hptrd_left.gif (955 bytes)
   dlg.SetItemValue "invoicedate", Now hptrd_left.gif (955 bytes)
   dlg.SetItemValue "quantity", 1
   dlg.SetItemValue "description", "Royalties for WSH Book"
   dlg.SetItemValue "price", "<lots of money>"
   dlg.SetItemText "total", "0"
   dlg.SetItemText "lire", "<i>No lire</i>"
   dlg.SetItemText "companysite", "our web site."
   dlg.SetItemHref "companysite", "http://www.expoware.com"
   dlg.SetItemValue "recipient", "Wrox Press" & vbCrLf & _ hptrd_left.gif (955 bytes)
                                 "Arden House" & vbCrLf & _
                                 "1102 Warwick Road, Acock's Green" & _
                                 vbCrLf & "Birmingham, UK. B27 6BH"
   dlg.Move 0, 0, 700, 450 
   If dlg.Show("center") = True Then
      MsgBox "You sent an invoice to:" & vbCrLf & vbCrLf & _
               dlg.GetItemValue("recipient") & vbCrLf & vbCrLf & _
             "for an amount payable of " & _
               dlg.GetItemText("lire"),, "New Invoice" hptrd_left.gif (955 bytes)
   End If
Else
   MsgBox "Template not found.", 64, "New Invoice"
End If
rainbow.gif (2243 bytes)
  • access WSHKIT which contains dialog component
  • "path://" means get from same directory as this script
  • Initialize fields to customize this dialog
  • Probably should get from data base
  • SET DATE TO TODAY
  • Probably should get from data base
  • Retrieve entered data and display it

Super Demo (chapter 10)

Description: invoice entry program which uses a data base, word document, and e-mail with input coming form a HTML dialog box (DESCRIBED earlier).

  • Get next invoice number to assign
  • Get input from user using HTML idalog
  • create WORD document from template using input from user
  • Update the Database
  • Print the document
  • Send the document to the final recipient

(Because ACCESS is not installed on my machine, I'll comment out the data base parts)

 

' NewInvoice.vbs
' Creates, prints and sends invoices through e-mail
' --------------------------------------------------------------
Option Explicit

' Constants
Const adUseClient = 3
Const adLockOptimistic = 3
Const ssfDrives = &H11                 ' Reference to My Computer
Const myInvoiceFolder = "C:\Invoices\"
Const myInvoiceDlgTemplate = "invoice.htm"
Const myInvoiceDocTemplate = "InvoiceTemplate.dot"

' --------------------------------------------------------------
' Gets the number for the new invoice
' --------------------------------------------------------------
Dim rs, numNewInvoice
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient

numNewInvoice = 1
rs.Open "select InvoiceNo from Invoices order by InvoiceNo desc", "Invoices"

If rs.RecordCount > 0 Then
   numNewInvoice = rs("InvoiceNo") + 1
End If

rs.Close

' --------------------------------------------------------------
' Collects all the information needed to prepare the invoice
' --------------------------------------------------------------
Dim dlg
Set dlg = CreateObject("WshKit.DialogBox")
If Not dlg.Create("path://" & myInvoiceDlgTemplate) Then
   MsgBox "Unable to locate the [" & myInvoiceDlgTemplate & "] template"
   WScript.Quit
End If

Dim sMonth, sDay, sYear, sDate
sMonth = MonthName(Month(Date))
sDay = Day(Date)
sYear = Year(Date)
sDate = sMonth & " " & sDay & ", " & sYear

' Initialize the fields of the dialog
dlg.SetItemSrc "companylogo", "expoware.gif"
dlg.SetItemText "companyname", "Expoware Soft"
dlg.SetItemText "companyaddress", "One Expoware Way, 1"
dlg.SetItemText "companycity", "00000 Roundmond"
dlg.SetItemText "companycountry", "Rome, Italy"
dlg.SetItemText "companyphone", "+39 06 12345678"
dlg.SetItemValue "invoiceID", numNewInvoice
dlg.SetItemValue "invoicedate", sDate
dlg.SetItemValue "quantity", 1
dlg.SetItemValue "description", "Description of the invoice"
dlg.SetItemText "companysite", "our Web site."
dlg.SetItemHref "companysite", "http://www.expoware.com"

' Display the dialog box
dlg.Move 0, 0, 700, 450
If Not dlg.Show("center") Then
   WScript.Quit
End If

' Store the data into variables
Dim dlgInvoiceNo, dlgInvoiceDate, dlgQuantity
Dim dlgPrice, dlgDesc, dlgTotal 
Dim dlgTo, dlgCurrency

dlgTo = dlg.GetItemValue("recipient")
dlgInvoiceNo = dlg.GetItemValue("invoiceID")
dlgInvoiceDate = dlg.GetItemValue("invoiceDate")
dlgQuantity = dlg.GetItemValue("quantity")
dlgDesc = dlg.GetItemValue("description")
dlgPrice = FormatNumber(dlg.GetItemValue("price"),,,, -2)
dlgTotal = dlgQuantity * dlgPrice
dlgTotal = FormatNumber(dlgTotal,,,, -2)

' Get the currency
Dim wbdoc, ukp
Set wbdoc = dlg.Document
Set ukp = wbdoc.all("ukp")
dlgCurrency = "$"
If ukp.checked Then dlgCurrency = "£"

' --------------------------------------------------------------
' Creates a new Word document
' --------------------------------------------------------------
Dim word, doc, docName
Set word = CreateObject("Word.Application")
Set doc = word.Documents.Add(myInvoiceDocTemplate)

' Fill the bookmarks in the document
doc.Bookmarks("InvoiceNumber").Range.Text = dlgInvoiceNo
doc.Bookmarks("InvoiceDate").Range.Text = dlgInvoiceDate
doc.Bookmarks("Paid").Range.Text = "PAID"
doc.Bookmarks("Quantity").Range.Text = dlgQuantity
doc.Bookmarks("Recipient").Range.Text = dlgTo
doc.Bookmarks("Description").Range.Text = dlgDesc
doc.Bookmarks("Price").Range.Text = dlgCurrency & dlgPrice
doc.Bookmarks("Total").Range.Text = dlgCurrency & dlgTotal
doc.Bookmarks("Payable").Range.Text = dlgCurrency & dlgTotal

' Save the document as 'Invoice #.doc'
Dim myDocs, fso
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(myInvoiceFolder) Then
   fso.CreateFolder(myInvoiceFolder)
End If

docName = myInvoiceFolder & "Invoice " & dlgInvoiceNo & ".doc"
doc.SaveAs docName
doc.Close
word.Quit

' --------------------------------------------------------------
' Records the invoice in the database
' --------------------------------------------------------------
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.Open "select * from Invoices", "Invoices",, adLockOptimistic
rs.AddNew

rs.Fields("InvoiceNo") = dlgInvoiceNo
rs.Fields("InvoiceDate") = dlgInvoiceDate
rs.Fields("Recipient") = dlgTo
rs.Fields("InvoiceDescription") = dlgDesc
rs.Fields("InvoiceAmount") = dlgTotal
rs.Fields("Currency") = dlgCurrency
rs.Fields("Paid") = 1
rs.Fields("WordFile") = docName
rs.Update
rs.Close

' --------------------------------------------------------------
' Prints the invoice
' --------------------------------------------------------------
Dim shell, f, fi

Set shell = CreateObject("Shell.Application")
Set f = shell.Namespace(ssfDrives)
Set fi = f.ParseName(docName)

fi.InvokeVerb("&Print")

' --------------------------------------------------------------
' Sends the document through e-mail
' --------------------------------------------------------------
' Displays a dialog to accept the e-mail address of the recipient. It also
'  validates what you type in using a regular expression.
Dim address
Set dlg = CreateObject("WshKit.InputBox")
dlg.Title = "Send E-mail"
dlg.Label = "Type the e-mail address in the edit box below:"
dlg.Format = "\w+\@\w+\.\w+"      ' The reg. exp. for e-mail addresses
dlg.Show

If dlg.Text = "" Then
   WScript.Quit
End If

' Stores the address
address = dlg.Text

Dim oApp
Dim mail
Const olMailItem = 0              ' A mail item in Outlook

' Starts the Outlook application
Set oApp = CreateObject("Outlook.Application")

' Creates a new mail item
Set mail = oApp.CreateItem(olMailItem)

' Fill in the message
mail.To = address
mail.Subject = "Invoice"
mail.Body = "Here's the invoice you were waiting for."
mail.Attachments.Add docName
mail.Send

' Closes Outlook
oApp.Quit

 



Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: April 13, 2000.