Skip navigation

10 More VBScript Techniques

Continue to build your scripting arsenal

In my June 2001 column, I shared 10 basic VBScript techniques. For those who want to step up a level and begin writing productive administrative scripts, here are 10 more VBScript techniques.

10.

On Error—The On Error statement lets a script trap runtime errors and continue executing. You can test for errors in the script after each statement has executed.

On Error Resume Next

9.

InStr—This function lets you locate a substring in a string. The function returns the starting position of the substring or a 0 if the function doesn't find the string. In

nPos = InStr("123345", "33")

nPos has a value of 3 because "33" begins in the third position of "123345."

8.

The Do Loop—This basic mechanism for repeatedly executing a set of statements comes in two forms: a Do Until Loop and a Do While Loop. The most important distinction between the two loops is that the Do Until Loop always executes at least once.

Do Until myValue > 1
  myValue = myValue + 1
Loop

7.

Subroutines—Modularizing your code into subroutines lets you organize your scripts and create reusable routines. You can define subroutines anywhere in a script. You use subroutines when you don't need to return a value to the calling code.

Sub mySub(myValue)
  MsgBox "Inside mySub" & myValue
End Sub

6.

Functions—Functions are routines that return a value to the calling code. The routine assigns the returned value to the function name.

Function myFunction(myParm)
  myFunction = myParm + 1
End Function

5.

MsgBox—In addition to displaying a message, the MsgBox function can display buttons and return the value of the selected button. The following example displays a message box that contains Yes, No, and Cancel buttons. The script assigns the selected button's value to nButton.

nButton = MsgBox("Click a button", vbYesNoCancel)

4.

Select Case—Use the Select Case statement to compare an expression against several other expressions.

Select Case nButton
  Case vbYes MsgBox "Yes"
  Case vbNo MsgBox "No"
  Case Else MsgBox "Cancel"
End Select

3.

CreateObject—This statement lets you create an instance of a COM object inside a script. You must use the Set statement, in conjunction with the CreateObject statement, to create objects. To destroy an object, set it to Nothing. The following example creates an instance of Microsoft Word:

Set myWord = CreateObject_
("Word.Application")

2.

Object properties—To read and write to one of an object's properties, type the object's name, a dot, and the property name. The following example sets the Word object's Visible property to True, causing the system to display the Word application:

myWord.Visible = True

1.

Object methods—You also use the dot notation to access an object's methods. The following example uses the Add method to create a new Word document and shows how to use the TypeText method to insert text:

myWord.Documents.Add
myWord.Selection.TypeText "Text from vbsample.vbs"
^^^^ ^^^
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