Skip navigation

Scripting Solutions with WSH and COM: Using WSH Drag and Drop to Send Faxes in Win2K

Downloads
23041.zip

You know that you can run many scripts from the command line, which you can access in two ways. You can select Run on the Start menu, type

cmd

(for Windows XP, Windows 2000, and Windows NT) or

command

(for Windows Me and Windows 9x), then click OK. Or you can launch cmd.exe (in XP, Win2K, and NT) from the \winnt\system32 folder or command.com (in Windows Me and Win9x) from the C:\ folder. A simple example of a batch (.bat) or command (.cmd) script that you can launch is echome.cmd (or echome .bat), which looks like this:

ECHO %1
PAUSE

When you call this script by typing

ECHOME hello-there

at the command line, it prints hello-there on the screen and waits for you to press a key before continuing.

You also know that you can double-click a script to execute it. However, an even simpler way to execute Windows Script Host (WSH) scripts is to simply drag another script file onto any WSH script you want to execute. When you execute any WSH script in this way, the WSH script starts up and takes the full file pathnames from the files you dragged on the script as input parameters.

WSH Drag and Drop
WSH drag and drop is easy to implement. Take a look at Figure 1, page 2. I've selected DragAnd Drop2.vbs and Win2KFax.vbs; when I drag both of these scripts onto DragAnd Drop1.vbs, Web-exclusive Figure 1 and Web-exclusive Figure 2 (which you can access at http://www.winscriptingsolutions.com, Instant Doc ID 23041) appear displaying the full path-name of the two dragged files, respectively. The simple script DragAndDrop1.vbs, which Listing 1 shows, enables this drag-and-drop functionality.

To trap errors in a WSH script, DragAnd Drop1.vbs must first ensure that errors don't cause the DragAndDrop1.vbs script to stop execution. Thus, DragAndDrop1.vbs uses the On Error Resume Next command first. For example, if access to the first argument in the DragAndDrop1.vbs script fails, the DragAnd Drop1.vbs script generates an error. However, because DragAndDrop1.vbs uses the On Error Resume Next command, the DragAndDrop1 .vbs script traps the error rather than stopping execution. Next, DragAndDrop1.vbs checks for errors. If an error occurred, the DragAndDrop1 .vbs script terminates. DragAndDrop1 .vbs uses a For Each...Next loop to iterate through the arguments and echo each one to the screen.

To make things a little more interesting, I've included a second, more complex drag-and-drop script in Listing 2, DragAndDrop2.vbs. This script is more complex than DragAndDrop1 .vbs for several reasons: First, the action to be taken on each WSH script resides in a separate subprocedure. Second, the DragAndDrop2.vbs script asks for a parameter if you don't provide one. Third, the DragAndDrop2 .vbs script removes the file path from each argument, just leaving you with the filename. Let's see how these changes work.

DragAndDrop2.vbs contains a subprocedure called Manipulate—the code at callout C in Listing 2. This subprocedure takes one parameter—a string called strFilePath—selects the filename from the string, and uses the WScript::Echo method to print the filename. I use the Split() function with the backslash (\) character as the delimiter to split the path into constituent parts and place those parts into an array called arrSplit. This array has an index that goes from 0 to the upper bound of the array, which the UBound(arrSplit) function determines. For the path C:\my scripts\win2kfax .vbs, the array looks like this:

arrSplit(0) = "C:"
arrSplit(1) = "my scripts"
arrSplit(2) = "win2kfax.vbs"

DragAndDrop2.vbs selects the last item in the array, which will be the filename, by using the Ubound() function to reference the array:

arrSplit(UBound(arrSplit))

which is the same as

arrSplit(2)

Although I split up the path and print out the filename in the body of the subprocedure, you can replace the lines in this subprocedure with whatever operations and procedures you want to perform on the file path you're passing in.

Now that you know what happens to each file that you pass in, let's look at how the main part of DragAndDrop2 .vbs works. To begin, the DragAnd Drop.vbs script calls the first argument again to see whether an error is generated. Unlike DragAndDrop1.vbs, however, if an error occurs, the Drag AndDrop2.vbs script goes to the code at callout A and requests a filename and path from the user. I suggest a default of C:\my scripts\draganddrop2 .vbs. If the user clicks Cancel or if the parameter comes back blank, the Drag AndDrop2.vbs script quits. Otherwise, the script passes the argument to the Manipulate subprocedure. If the original attempt to call the first argument was successful, the DragAndDrop2 .vbs script moves to the code at callout B. The code at callout B is another For Each...Next loop such as in DragAnd Drop1.vbs, but this time the DragAnd Drop2.vbs script passes the parameter to the Manipulate subprocedure each time.

Using Drag and Drop with the Win2K Fax Service
Now that you know how DragAnd Drop2.vbs works, let's extend the script to a real-world example. All versions of Win2K come with software that lets you send faxes easily and inexpensively. I've written a script called Win 2KFax.vbs, which Listing 3 shows, to automate faxing from Win2K.

To run this script, you need a Win2K machine with the Fax Service installed and enabled. The script runs with the default versions of the scripting engines that come with Win2K (i.e., without any service packs installed).

Win2KFax.vbs requires four parameters to the Win2K Fax Service to send a fax: the file to send, the recipient, the number to send the fax to (including any prefix for an outside line, such as 9), and a unique reference ID that you specify to identify the fax. You pass in the first parameter by using the drag-and-drop method that I've shown you. The script captures the other three parameters from information the user enters in standard input boxes.

Now, let's look at how Win2K Fax.vbs works. First, the script defines the four constants that it's going to use. The first constant is the path to the fax server. (You need to replace "\\SERVER1" with your server's name.) The second constant is the text string that holds the name of your business. The third constant represents the value needed to specify that the open file is for append and not overwrite. The final constant is a log file.

After declaring the variables, the script verifies that it has received the drag-and-drop argument. If it hasn't, the script writes an error and quits. If the script has the file argument, it places that argument and the three parameters that it gathered through the input boxes into four corresponding variables. (Note that you don't have to perform this step: You could reference the arguments directly.)

The script needs to connect to the fax server COM object to automate it, so I use the WScript::CreateObject method to get a handle to that object. I then use the FaxServer::Connect method of that object to connect to the Fax Service running on the server (SERVER1, in this case) by passing the server name in as a parameter to the method call. To send the document, I first need to use the fax object's IFaxServer::CreateDocument method to create a document. I pass in as a parameter the file whose contents I'll be sending, receive a handle back to the document itself—objDocument —then add a few other items before sending. The next three lines use the IFaxDoc::DisplayName, IFaxDoc::Sender Company, and IFaxDoc::FaxNumber methods, respectively, to set the display name for the fax in the queue, the sending company name, and the fax number that I'm sending to, respectively. The last two methods are straightforward, but the first one can be whatever you want. (I use the recipient's name and the fax reference ID.)

Now, the script uses the IFaxDoc ::Send method to send the fax and the FaxServer::Disconnect method to disconnect from the fax server. The last four lines connect to the Scripting:: FileSystemObject, open the log file specified in the constant at the beginning, write a line out to the log file, and close the file. What you write here is up to you, but I use the same items that I used in the display name.

You can extend Win2KFax.vbs to send more than one file to the same place. To do so, you use the construct

For Each argInput In _
   WScript.Arguments
     ...
Next

in the middle section of the script.

This code ensures that the script acts on each file that was sent in. Whether you use the same fax number, recipient, and reference ID for all files that you dragged on or were prompted for each one is up to you. To prompt for a value for each file, you just put each InputBox() function inside the For Each...Next loop. Experiment with adapting Win2KFax.vbs and see what you can do.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish