DOWNLOAD THE CODE:
Download the Code 26972.zip

Emailing a Document
The Word object model includes a method on the Document object that lets you send the document through email:

doc.SendMail

As you can see, the SendMail method takes no arguments. The method calls the registered Messaging API (MAPI) client—typically Microsoft Outlook or Outlook Express. An email-application message window eventually appears with the Word document appended as an attachment. The user simply fills the To field and clicks Send. The mail program's Sent Items folder automatically traces the message. If the user doesn't have a full-time connection, the mail program puts the message into the Outbox folder to be sent the next time the user connects.

When you use the SendMail method, you need to be sure that your script doesn't close the Word application before the email window appears. The SendMail method operates asynchronously with respect to the main script, so the script has no way of determining that the SendMail method has finished sending the message. If your script attempts to close Word before the SendMail method has finished, the system generates a message saying that you can't close Word until the command is finished. If this message box appears, the user must close it manually. To avoid the system-generated message box, you can add a call to Sleep, as I do within callout B in Listing 2, to temporarily stop the script and give the SendMail method time to complete. Alternatively, you can add a custom confirmation message box to your script so that the user can confirm that the email has been sent. The following code displays a confirmation message box called Done just before the script closes Word:

doc.SendMail
MsgBox "Done"

Run with It
Most forms today are simple Word documents. After you interactively create a template in Word, you can easily write a script such as the one I provide here to populate the form with custom data, then let WSH and the Word object model do the rest.

After you see how simple and effective this approach is, you'll be tempted to use it for other frequently used forms, such as expense reports and subscription requests. You can also add functionality to your scripts. For example, you might want to integrate automatic invoice creation with database access so that your script can automatically retrieve the next available invoice number. Or you might want your script to add a record to a database to track the invoice. To do so, you can extend CreateInvoice.vbs with ADO-based code that queries the database for the latest invoice number and executes an INSERT statement to add a new record. To learn more about using ADO for database access, see "Rem: Obtaining Data from a SQL Server Database," August 2002, http://www.winscriptingsolutions.com, InstantDoc ID 25628.

End of Article

Prev. page     1 2 [3]     next page -->



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

very good documentation

coyotes

Article Rating 3 out of 5

 
 

ADS BY GOOGLE