Vbscript Programmer’S Reference Third Edition
Total Page:16
File Type:pdf, Size:1020Kb
VBScript Programmer’s Reference Third Edition Adrian Kingsley-Hughes Kathie Kingsley-Hughes Daniel Read Wiley Publishing, Inc. ffirs.indd iii 8/28/07 9:41:21 AM ffirs.indd vi 8/28/07 9:41:22 AM VBScript Programmer’s Reference Third Edition Introduction . xxv Chapter 1: A Quick Introduction to Programming . 1 Chapter 2: What VBScript Is — and Isn’t! . 31 Chapter 3: Data Types . 45 Chapter 4: Variables and Procedures . 83 Chapter 5: Control of Flow . 109 Chapter 6: Error Handling and Debugging . 129 Chapter 7: The Scripting Runtime Objects . 183 Chapter 8: Classes in VBScript (Writing Your Own COM Objects) . 209 Chapter 9: Regular Expressions . 233 Chapter 10: Client-Side Web Scripting . 261 Chapter 11: Windows Sidebars and Gadgets . 287 Chapter 12: Task Scheduler Scripting . 309 Chapter 13: PowerShell . 345 Chapter 14: Super-Charged Client-Side Scripting . 375 Chapter 15: Windows Script Host . 405 Chapter 16: Windows Script Components . 465 Chapter 17: Script Encoding . 489 Chapter 18: Remote Scripting . 509 Chapter 19: HTML Applications . 517 Chapter 20: Server-Side Web Scripting . 535 Chapter 21: Adding VBScript to Your VB and .NET Applications . 569 (Continued) ffirs.indd i 8/28/07 9:41:21 AM Appendix A: VBScript Functions and Keywords . 603 Appendix B: Variable Naming Convention . 675 Appendix C: Coding Conventions . 677 Appendix D: Visual Basic Constants Supported in VBScript . 681 Appendix E: VBScript Error Codes and the Err Object . 687 Appendix F: The Scripting Runtime Library Object Reference . 703 Appendix G: The Windows Script Host Object Model . 715 Appendix H: Regular Expressions . 723 Appendix I: The Variant Subtypes . 727 Appendix J: ActiveX Data Objects . 731 Index . 759 ffirs.indd ii 8/28/07 9:41:21 AM VBScript Programmer’s Reference Third Edition Adrian Kingsley-Hughes Kathie Kingsley-Hughes Daniel Read Wiley Publishing, Inc. ffirs.indd iii 8/28/07 9:41:21 AM VBScript Programmer’s Reference, Third Edition Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-16808-0 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data Kingsley-Hughes, Adrian. VBScript programmer’s reference / Adrian Kingsley-Hughes, Kathie Kingsley-Hughes, Daniel Read. p. cm. Includes index. ISBN 978-0-470-16808-0 (paper/website) 1. VBScript (Computer program language) 2. HTML (Document markup language) 3. World Wide Web. I. Kingsley-Hughes, Kathie. II. Read, Daniel, 1969– III. Title. IV. Title: VB Script programmer’s reference. QA76.73.V27K56 2007 005.2' 762—dc22 2007028895 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-8600. 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-4355, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disap- peared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respec- tive owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. ffirs.indd iv 8/28/07 9:41:22 AM To my kids—you guys are great! —Adrian To my parents, for their loving support and enduring patience. And to my kids, for being just so cool! —Kathie ffirs.indd v 8/28/07 9:41:22 AM ffirs.indd vi 8/28/07 9:41:22 AM About the Authors Adrian Kingsley-Hughes has made his living as a technology writer for over a decade, with many books and articles to his name. He can also be found teaching classes on the Web, where he has success- fully taught technology skills to thousands of learners, with his own special brand of knowledge, experi- ence, wit, and poor spelling. He is also editor of the ZDNet blog Hardware 2.0 ( http://blogs.zdnet .com/hardware ). Kathie Kingsley-Hughes has worked in IT training for many years. In addition to writing, she now works as a courseware developer and e-trainer, specializing in Internet technologies. She also runs a web development company in the United Kingdom. Daniel Read is a software developer living and working in Atlanta, GA, USA. He currently works for Connecture Inc., an Atlanta-based software consulting firm specializing in the insurance industry. Daniel also publishes and writes essays for developers at DeveloperDotStar.com, a web-based magazine for software professionals. ffirs.indd vii 8/28/07 9:41:22 AM ffirs.indd viii 8/28/07 9:41:23 AM Credits Acquisitions Editor Vice President and Executive Group Publisher Katie Mohr Richard Swadley Development Editor Vice President and Executive Publisher Maureen Spears Joseph B. Wikert Technical Editor Project Coordinator, Cover Andrew Moore Adrienne Martinez Copy Editor Proofreader Mildred Sanchez Ian Golder Editorial Manager Indexer Mary Beth Wakefield Johnna VanHoose Dinse Production Manager Anniversary Logo Design Tim Tate Richard Pacifico ffirs.indd ix 8/28/07 9:41:23 AM ffirs.indd x 8/28/07 9:41:23 AM Acknowledgments Writing a book is hard work, and writing a third edition is even harder! The process involves a lot more people than just those listed on the cover (although thanks must got to Kathie and Dan for their hard work). My sincerest thanks goes out to everyone who made this book possible, from the initial idea of revamping this title for a second time to getting it onto the shelves. —Adrian Many thanks to family, friends, and colleagues, who have been very supportive during the writing of this book. A big thank you to all the editors, tech reviewers, and production staff who worked so hard on this edition. —Kathie I thank my fellow authors, Adrian and Kathie, and also the fine editorial staff at Wiley. —Daniel ffirs.indd xi 8/28/07 9:41:23 AM ffirs.indd xii 8/28/07 9:41:23 AM Contents Acknowledgments xi Introduction xxv Chapter 1: A Quick Introduction to Programming 1 Variables and Data Types 2 Using Variables 2 Using Comments 4 Using Built-in VBScript Functions 5 Understanding Syntax Issues 6 Flow Control 9 Branching 9 Looping 14 Operators and Operator Precedence 18 Organizing and Reusing Code 19 Modularization, Black Boxes, Procedures, and Subprocedures 20 Turning Code into a Function 21 Advantages to Using Procedures 23 Top-Down versus Event-Driven 23 Understanding Top-Down Programming 24 Understanding Event-Driven Programming 24 How Top-Down and Event-Driven Work Together 24 An Event-Driven Code Example 25 Coding Guidelines 25 Expect the Unexpected 26 Always Favor the Explicit over the Implicit 27 Modularize Your Code into Procedures, Modules, Classes, and Components 27 Use the “Hungarian” Variable Naming Convention 28 Don’t Use One Variable for More Than One Job 28 Always Lay Out Your Code Properly 28 Use Comments to Make Your Code More Clear and Readable, but Don’t Overuse Them 29 Summary 29 Chapter 2: What VBScript Is — and Isn’t! 31 Windows Script 31 Version Information 32 ftoc.indd xiii 8/28/07 2:30:19 PM Contents VBScript Is a Subset of VB 32 VBScript Is a Scripting Language 33 VBScript Is Interpreted at Runtime 33 Runtime Compilation — Disadvantages 34 Runtime Compilation — Advantages 35 Advantages of Using VBScript 36 Is VBScript Right for You? 37 How VBScript Fits