Wrox VBScript Programmers Reference Dec 1999 ISBN 0764543679 pdf

  

Table of Contents

  

VBScript Programmer's Reference...................................................................................................................1

Introduction.........................................................................................................................................................6

  Who is this Book For?............................................................................................................................6 How to Use this Book.............................................................................................................................6 What is VBScript?..................................................................................................................................7 What Can You Do With VBScript?......................................................................................................11 What Tools Do You Need to Use VBScript?.......................................................................................14 What's New in VBScript 5?..................................................................................................................15 Code Conventions.................................................................................................................................17 Tell Us What You Think.......................................................................................................................17 Customer Support.................................................................................................................................18

  

Chapter 1: A (Very) Short Introduction to Programming..........................................................................19

Overview...............................................................................................................................................19 Variables and Data Types.....................................................................................................................19 Control of Flow.....................................................................................................................................23 Operators...............................................................................................................................................31 Organizing and Reusing Code..............................................................................................................32 Top−Down vs. Event Driven................................................................................................................35 Some Guidelines to Keep in Mind........................................................................................................37 Suggestions for Further Reading..........................................................................................................39

Chapter 2: Variables and Data Types............................................................................................................40

The Visual Basic Data Types................................................................................................................40 The Variant: VBScript's Only Data Type.............................................................................................42 Variables in VBScript...........................................................................................................................60 Literals and Named Constants..............................................................................................................65 Complex Data Types.............................................................................................................................69

Chapter 3: Control of Flow.............................................................................................................................77

Branching Constructs............................................................................................................................77 Loop Constructs....................................................................................................................................81

Chapter 4: Error Handling, Prevention and Debugging.............................................................................93

Overview...............................................................................................................................................93 Introduction...........................................................................................................................................93 Seeing the Error of your Ways..............................................................................................................94 Different Types of Errors......................................................................................................................98 What Can We Do About Errors?........................................................................................................105 Debugging...........................................................................................................................................120 Common Errors and How To Avoid Them........................................................................................127 Summary.............................................................................................................................................128

Chapter 5: Using COM Components and Objects.....................................................................................129

Overview.............................................................................................................................................129 Introduction to COM components and Objects..................................................................................129 Object and Object Interfaces...............................................................................................................131 What COM Components and Objects are Available?........................................................................137 Objects and the Variant data type.......................................................................................................138

  

Table of Contents

   Chapter 5: Using COM Components and Objects Properties and Methods.......................................................................................................................139 Object Scope, Lifetime and References..............................................................................................141 Using the With Statement with Objects..............................................................................................145 Create your own COM Objects...........................................................................................................146 Summary.............................................................................................................................................146

Chapter 6: Using COM Components with MTS.........................................................................................147

Overview.............................................................................................................................................147 A Quick Introduction to MTS.............................................................................................................147 Advantages of using MTS..................................................................................................................149 Next Step: COM+...............................................................................................................................158 Summary.............................................................................................................................................160

Chapter 7: The Built−In and Scripting Runtime Objects..........................................................................162

Overview.............................................................................................................................................162 Runtime vs. Built−In − What's the Difference?..................................................................................162 Built−In Objects..................................................................................................................................162 Regular Expressions............................................................................................................................163 Scripting Runtime Objects..................................................................................................................173 Summary.............................................................................................................................................182

Chapter 8: Classes in VBScript (Writing Your Own COM Objects).......................................................183

Classes vs. Objects vs. Components...................................................................................................183

Chapter 9: Windows Script Components....................................................................................................204

What Are Windows Script Components?...........................................................................................204 What Tools Do You Need?.................................................................................................................204 The Script Component Runtime.........................................................................................................205 Script Component Files......................................................................................................................206 Exposing Properties, Methods, and Events.........................................................................................211 Creating Registration Information......................................................................................................214 Creating the Script Component Type Library....................................................................................216 Interface Handlers...............................................................................................................................218 Compile−time Error Checking............................................................................................................220 Using VBScript Classes in Script Components..................................................................................220 Summary.............................................................................................................................................227

Chapter 10: The Windows Script Host........................................................................................................228

Overview.............................................................................................................................................228 What Tools Do You Need?.................................................................................................................228 What is the Windows Script Host?.....................................................................................................229 Running Scripts with Windows Script Host.......................................................................................230 Using .WSH Files to Launch Scripts..................................................................................................232 Windows Script Host Intrinsic Objects...............................................................................................232 Summary.............................................................................................................................................263 Additional Resources..........................................................................................................................263

  

Table of Contents

  

Chapter 11: General Client−Side Web Scripting.......................................................................................265

What Tools Do You Need?.................................................................................................................265 How Browser Scripting Works...........................................................................................................265 The Various Scripting Languages − What's Best for the Browser.....................................................266 Responding to Browser Events...........................................................................................................268 Validating Forms................................................................................................................................272 The Document Object Model..............................................................................................................276 Summary.............................................................................................................................................280

Chapter 12: High−Powered Client Scripting..............................................................................................282

Technology Requirements..................................................................................................................282 Importance of Browser Security Settings...........................................................................................282 Scriptlets − Ancestors of Behaviors....................................................................................................282 Managing Events................................................................................................................................288 Behaviors............................................................................................................................................292 HTML Components............................................................................................................................295 Remote Scripting................................................................................................................................304 Summary.............................................................................................................................................310

Chapter 13: HTML Applications (HTAs)...................................................................................................311

What Tools Do You Need?.................................................................................................................311 What is an HTML Application?.........................................................................................................311 How to Create a Basic HTA...............................................................................................................312 The <HTA:APPLICATION> Tag......................................................................................................314 HTAs and Security..............................................................................................................................320 HTA Deployment Models...................................................................................................................324 What Isn't Supported With HTAs?.....................................................................................................326 Summary.............................................................................................................................................327

Chapter 14: Server−Side Web Scripting With ASP...................................................................................328

Overview.............................................................................................................................................328 The Anatomy of the HTTP Protocol...................................................................................................328 Introducing Active Server Pages........................................................................................................333 The Active Server Pages Object Model..............................................................................................336 Using Active Server Pages Effectively...............................................................................................352 Summary.............................................................................................................................................358

Chapter 15: Talking to Databases: ActiveX Data Objects.........................................................................359

What Tools Do You Need?.................................................................................................................359 The Evolution of ADO........................................................................................................................359 What Are ActiveX Data Objects?.......................................................................................................360 The ADO Objects...............................................................................................................................361 Summary.............................................................................................................................................374

Chapter 16: Microsoft Script Control..........................................................................................................375

Why Script your Application?............................................................................................................375 What tools do you need?.....................................................................................................................376 Adding Script Control to VB Application..........................................................................................377 Macro and Scripting Concepts............................................................................................................380 The Script Control Object Model.......................................................................................................381

  

Table of Contents

   Chapter 16: Microsoft Script Control Other Scripting Elements....................................................................................................................396 Error Trapping with Script Control.....................................................................................................397 Sample Applications...........................................................................................................................399 Summary.............................................................................................................................................408

Appendix A: Visual Basic Functions and Keywords..................................................................................409

Overview.............................................................................................................................................409 Operators.............................................................................................................................................409 Math Functions...................................................................................................................................417 Date and Time Functions and Statements...........................................................................................422 Unsupported Array Functions and Statements...................................................................................435 Unsupported String Functions, Statements and Constructs................................................................449 String Constants..................................................................................................................................451 Conversion Functions.........................................................................................................................451 Unsupported conversion functions.....................................................................................................456 Miscellaneous Functions, Statements and Keywords.........................................................................457

Appendix B: Differences between VB/VBA and VBScript5......................................................................471

Appendix C: Code Conventions...................................................................................................................474

Variable Naming Conventions............................................................................................................474 Procedure Naming..............................................................................................................................475 Indentation..........................................................................................................................................475 Commenting........................................................................................................................................476

Appendix D: Visual Basic Constants Supported in VBScript...................................................................477

Color Constants...................................................................................................................................477 Date and Time Constants....................................................................................................................477 Date Format Constants........................................................................................................................478 Miscellaneous Constants.....................................................................................................................478 MsgBox Constants..............................................................................................................................478 String Constants..................................................................................................................................479 Tristate Constants...............................................................................................................................479 VarType Constants..............................................................................................................................479

Appendix E: VBScript Error Codes and the Err Object...........................................................................481

Runtime Errors....................................................................................................................................481 Syntax Errors......................................................................................................................................482 Err Object and On Err statement.........................................................................................................484

Appendix F: The Scripting Runtime Library Objects Reference.............................................................494

The Scripting.Dictionary Object.........................................................................................................494 The Scripting.FileSystemObject Object.............................................................................................495 The Scripting.TextStream Object.......................................................................................................500

Appendix G: Windows Script Host 2.0........................................................................................................502

  

Table of Contents

Appendix H: The Browser Object Model IE4............................................................................................514

  The Object Model In Outline..............................................................................................................514 HTML and Form Controls Cross Reference.......................................................................................530

  

Appendix I: The Browser Object Model IE5.............................................................................................533

  The Object Model In Outline..............................................................................................................533 HTML and Form Controls Cross Reference.......................................................................................545

  

Appendix J: The Integral ASP Objects.......................................................................................................548

  The ASP Object Model.......................................................................................................................548 The Application Object.......................................................................................................................549 The ASPError Object..........................................................................................................................550 The Request Object.............................................................................................................................550 The Response Object..........................................................................................................................551 The Server Object...............................................................................................................................553 The Session Object.............................................................................................................................554

  

Appendix K: ADO Object Summary, Constants, and Data Types...........................................................557

  Microsoft ActiveX Data Objects 2.5 Library Reference....................................................................557 ADO Constants...................................................................................................................................571 ADO Data Types.................................................................................................................................605

  

Appendix L: The Microsoft Script Encoder................................................................................................611

VBScript Programmer's Reference Susanne Clark, Antonio De Donatis, Adrian Kingsley−Hughes, Kathie Kingsley−Hughes, Brian Matsik, Erick Nelson, Piotr Prussak Daniel Read, Carsten Thomsen, Stuart Updegrave, Paul Wilton

  Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2003 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada Library of Congress Card Number:

  ISBN: 0−7645−4367−9 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1

  1B/QZ/QW/QT/1N No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per−copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750−8400, fax (978) 646−8700. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572−3447, fax (317) 572−4447, E−Mail: permcoordinator@wiley.com

  Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in

  preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages.

  For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (800) 762−2974, outside the U.S. at (317) 572−3993 or fax (317) 572−4002.

  Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

  

Trademarks: Wiley, the Wiley Publishing logo, Wrox, the Wrox logo, the Wrox Programmer to Programmer

  logo and related trade dress are trademarks or registered trademarks of Wiley in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book

  Trademark Acknowledgements

  Wrox has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Wrox cannot guarantee the accuracy of this information.

  Credits Authors

  Daniel Read Susanne Clark Antonio De Donatis Adrian Kingsley−Hughes Kathie Kingsley−Hughes Brian Matsik Erick Nelson Piotr Prussak Carsten Thomsen Stuart Updegrave Paul Wilton

  Technical Reviewers

  Chris Behrens Steve Danielson John Granade Michael Harris Richard Harrison Dan Pfeffer Nic Roche

  Design / Layout

  Tom Bartlett Mark Burdett William Fallon Jonathan Jones John McNulty

  Additional Material

  Jerry Ablan Jeff Hart Alex Homer David Sussman Chris Ullman

  Illustrations

  William Fallon Jonathan Jones

  Editors

  Gregory Beekman Lums Thevathasan Robert Shaw Devin Lunsford

  Cover Design

  Chris Morris

  Project Manager

  Chandima Nethisinghe

  Managing Editor

  Victoria Hudgson

  Index

  Martin Brooks

  Development Editor

  Peter Morgan

  About the Authors Daniel Read

  Daniel Read is Senior Developer at Compass, Inc. in Atlanta, GA, where he and fellow team members build

  IIS/ASP/VB/MTS e−commerce web sites, as well as client/server and distributed systems. Daniel sometimes longs for his simpler, statically linked days as an X−Base developer and DOS command line junkie, but today enjoys the power afforded by the arsenal of Microsoft's Windows DNA tools and technologies. His other interests include camping, crime fiction, listening to music of all sorts, and going to as many concerts as he can. He's not sure if co−authoring a programming book will help him meet the woman of his dreams, but it sure would be nice.

  Carsten Thomsen

  Carsten Thomsen is a Microsoft Certified Systems Engineer, who started programming in Visual Basic back in 1993, when it was version 3.0. Presently he is pursuing Microsoft Certified Systems Developer certification and was recently awarded the MVP title for his contributions to the Microsoft Visual Basic newgroups. For the last 4 years he has been developing 2−tier and 3−tier client/server solutions based on MS SQL Server, MTS and MSMQ, but has more recently been moving towards browser−based development, based on MS IIS, using ASP/VBScript, COM/ActiveX components and Visual Basic WebClasses. In whatever spare time he has, he enjoys traveling and spending time with his two daughters, Nicole and Caroline, and his girlfriend Mia. He works out at a local gym and (at spectator−level) enjoys ice hockey and soccer. He is generally considered a computer freak, and spends too much time at the computer.

  Piotr Prussak

  Piotr Prussak works for ACEN in Buffalo, NY as a developer (although he is originally from Poland). He creates ASP, VB and Java Applications for the company and its clients. He also spends some time working with the SQL server and ColdFusion, probably more than he should. When he's not programming and mentoring, he either writes stuff, or edits stuff written by others. Sometimes he sleeps, eats and relaxes like any other human being. In a past life he used to be a photographer, but that was just too relaxing. Piotr (or Peter, as some might call him) is married to lovely Joanne, who is an MIS student at a local University. Sometimes they travel all over the world and are disconnected from the world of computers and 24 hour news.

  Brian Matsik

  Brian Matsik is the President and Senior Consultant at OOCS in Charlotte, NC. He is MCSD certified and has been working with Visual Basic, VBScript, and VBA for over six years. He currently specializes in ASP, SQL Server, and VB COM. Brian can be reached at brianmat@oocs.com. Brian would like to thank Tracy for her support, Mike Dunner for keeping him on his toes when it comes to ADO and MTS, and Mark Harris for taking care of the books while he stays glued to the PC.

  Paul Wilton

  After an initial start as a Visual Basic applications programmer, Paul found himself pulled into the net and has spent the last 18 months helping create internet and intranet solutions. Currently he is developing web−based systems, primarily using Visual Basic 6 and SQL Server 7, along with numerous other technologies. Paul would like to wish lots of love to his fiancée Catherine, who ensures his sanity chip remains plugged in.

  Antonio De Donatis

  Antonio De Donatis (adedonatis@yahoo.com) began programming in 1984, using a mixture of Basic and assembler on a Commodore 64. Since then, he has designed software the object−oriented way, employing more programming languages and technologies than he can count. He finds programming to be one of the best activities to exercise creativity and to be intellectually active. He has worked for many of the major firms in

  IT either as employee or as freelance. His current interests include all the technologies involved in the design and implementation of e−commerce solutions. Antonio holds a BS degree in Computer Science from Pisa University (Italy) and he is a Microsoft Certified Professional.

  Susanne Clark

  Susanne has worked with the latest internet technologies, including DHTML, XML, XSL, IE5 programming and scripting, and has contributed chapters on these subjects to the recently published Professional Visual

  InterDev 6 Programming

  from Wrox Press. She is currently working as a user interfaces developer in Seattle, USA. When not dealing with computers, she enjoys spending time with her cats, and reading mystery books. Susanne would like to thank Luca for all of his patience and help. Ti amo.

  Adrian Kingsley−Hughes

  Adrian Kingsley−Hughes is Technical Director of Kingsley−Hughes Development Ltd., a UK−based training and development firm, where he is a consultant in Internet Development and Windows platform programming. He has co−authored 8 books for Active Path and Wrox Press. His abiding passion is the Search for Extra Terrestrial Intelligence and he is the Welsh Regional Coordinator for SETILeague Inc. In his spare time he writes horror novels, plays the didgeridoo and is currently building a radio telescope in his back yard.

  Kathie Kingsley−Hughes

  In addition to writing web development books for Active Path and Wrox Press, Kathie Kingsley−Hughes is the Managing Director of UK−based Kingsley−Hughes Development Ltd., where she specializes primarily in the development and delivery of training courses in web development skills. She began teaching in 1985 and has lectured at several UK colleges and other training establishments and she is currently teaching at ZDU. Any spare time is taken up with astronomy, photography and hiking in the mountains.

  Eric Nelson

  Erick Nelson is an 18 year old Internet developer and programmer, and creator of the award winning website cues.com. about which he spoke at both the 1998 and 1999 Professional ASP Developer's Conference. Erick began his programming career at age 16, and now has experience using ASP, VBScript and Visual Basic to design sites.

  Erick enjoys playing computer games and working on his online RPG system. His outdoor activities include mountain biking, swimming, cliff jumping, playing tennis and snow skiing. He can be reached at erick@ericknelson.com. Erick would like to thank his Mom, Cindy Cashman, for helping him start his business, and his Dad, Mark Nelson, for teaching him how to build things and raising him better than anyone could under the circumstances. Thanks must also go to Bill Stroud and Charlie Bass.

Introduction

  The aim of this book is to provide an introduction to, overview of, and reference for:

  • The VBScript 5 language
  • The many 'contexts' in which the VBScript language can exist and be useful

  Our first goal probably seems pretty straightforward: we're going to cover what you need to know to write

  VBScript code. We'll go over general syntax, functions, keywords, style, error handling, and similar

  

language−specific topics. However, VBScript the language is of limited use without something to do. That's

where the scripting contexts (or hosts) come in.

  Microsoft has done an excellent job over the last couple of years in building robust support for scripting into the Windows operating systems (Windows 95/98, Windows NT, and now, Windows 2000). Support for scripting is an important aspect of a mature operating system environment. 'support for scripting' simply means that the operating system provides a mechanism through which users can write programs in a scripting language, such as VBScript. A scripting language differs from 'normal' programming languages in some key ways, which we will discuss in this introduction. Scripting allows you to extend the capabilities of an existing computer environment. This book will provide you with the knowledge you need to leverage all of this dormant power.

Who is this Book For?

  VBScript, and this Programmer's Reference book, can be extremely useful to all sorts of information technology professionals, and even for the casual user who wants to automate some common tasks or learn a little bit about the art and science of programming. For example:

  • • Network administrators can use VBScript with the Windows Script Host to write powerful login

  scripts and automate formerly time−consuming and error−prone tasks all without using clumsy DOS batch files

  • Application developers can use VBScript to extend their Windows applications with powerful macro

  and customization features

  • Web developers and designers can use VBScript with DHTML to create amazing browser−based and

  stand−alone HTML Applications

  • Web application developers can use VBScript to create dynamic, high−performance web sites with

  Internet Information Server and Active Server Pages

  These are just examples the full range of people and tasks for which VBScript can be useful cannot be expressed in a single paragraph. If you're not sure if this book is for you, or if VBScript can help you, please read on because VBScript can do a lot more than you might think.

How to Use this Book

  This book is divided up into three parts. Part 1 covers the VBScript language the keywords, functions, syntax, etc. For those who are new to writing code, Part 1 also includes a very short introduction to programming. Advanced readers might not need to read Part 1 straight through, but should probably read the chapters on VBScript's new support for classes and Windows Script Components, a very exciting new technology.

  Part 2 covers what we call 'VBScript in Context.' Although powerful in its own right, the VBScript language derives its real power from:

  • The hosts that support it such as the Windows Script Host (formerly known as the Windows

  Scripting Host) and Active Server Pages

  • The objects that are made available to it, such as the Scripting Runtime Objects and Active Data

  Objects

  You'll learn, among lots of other things, how to create an HTML Application and how to write a scriptlet and attach it to a DHTML behavior. At the end of the book there is a large set of appendices, which cover things such as naming conventions, the syntactic differences between Visual Basic and VBScript, and the object models that are important to

  VBScript developers. Although the chapters will cover these object collections in detail, the object diagrams provide a very helpful high level view of the objects and their properties and methods. Appendix A also contains a complete reference to the functions, keywords, and operators of the VBScript 5 language. Finally, don't forget that we, at Wrox, offer comprehensive on−line support and references. www.webdev.wrox.co.uk has an HTML listing and www.asptoday.com is a dedicated ASP resource center.

  Important Advanced readers who are already familiar with VBScript and the concept of a scripting language might want to skip these introductory sections. However, if you are not yet familiar with all of the new features of VBScript 5 and with the new types of solutions supported by Microsoft's latest scripting hosts, you might want to take a look at the

  What Can You Do With VBScript?

  and What's New in VBScript 5 sections of this Introduction.

What is VBScript?

  As its name suggests, VBScript is what's known as a scripting language. How is a scripting language different from other types of programming languages? One big difference is the point at which the code you have written is 'compiled'.

A Little Background

  All programming languages are compiled at some point otherwise the computer would not be able to respond to their commands. As you undoubtedly know, at the lowest level, the language of a computer is broken down into a series of 1's and 0's. This ones−and−zeroes language is also known as binary or machine language. Different combinations of 1's and 0's mean different things to the low−level hardware of the computer. One binary arrangement of 1's and 0's might mean add these two numbers together. Another might mean, store this value in such−and−such a memory address. This is an oversimplification, but hopefully you get the idea. It's pretty amazing, but all of the different things you can ask a computer to do are ultimately broken down into a fairly small set of machine language commands. There was once a time when programmers had to write computer programs in binary as you might imagine, this is pretty difficult. Over time, more advanced programming languages were developed, each of which added ever−higher levels of abstraction, so that programmers could use syntax that was a little closer to the English language. However, even as programming languages have become increasingly abstract, the computer hardware has continued to understand only machine language.

Scripting Languages are Compiled Later than Other Languages

  Compilation is the process of turning the higher−level language into the binary that the computer

  understands. The difference with 'normal' languages and scripting languages is not whether they are compiled but when. Languages like C and C++ are commonly known as compiled languages. This may seem a bit confusing since we just said that all languages are ultimately compiled, but they are called this because they are compiled down to machine code at design time. That is, the programmer writes the code, then issues a command to a special program called a compiler to compile the code down to machine language. A scripting language, on the other hand, is 'compiled' (or more precisely, 'interpreted') at runtime, which means that until it is executed, the script remains a plain text file. If you looked at a compiled C program, it would not make any sense, because it's been broken down into machine language. However, a script is always stored as a plain text file. You can write and read the script in plain text, change it at will, and just tell a script engine to run it. You do not have to go through a separate design−time step to compile the program. Certainly, there are special formatting conventions that you must follow when you write a script, but the code itself always remains plain text that you can read and edit in any text editor (such as the Notepad application that is included with all Windows versions).

  A script is compiled at runtime by a scripting engine, which is a special program that knows how to interpret the text you type into the script and turn it into commands that the computer can understand. In this respect, it is not unlike the C programmer's design−time compiler. Compiling a program at design time affords three major advantages, which, conversely, translate into disadvantages for a scripting language such as VBScript: