Name
With Statement
Syntax
Withobject
[statements
] End With
-
object
Use: Required
Data Subtype: Object
A previously declared object variable.
-
statements
Use: Optional
Program code to execute against object.
Description
Performs a set of property assignments and executes other code against a particular object, thus allowing you to refer to the object only once. Because the object is referred to only once, the “behind the scenes” qualification of that object is also performed only once, leading to improved performance of the code block.
Rules at a Glance
The single object referred to in the
With
statement remains the same throughout the code contained within theWith...End
With
block. Therefore, only properties and methods ofobject
can be used within the code block without explicitly referencing the object. All other object references within theWith...End
statement must start with a fully qualified object reference.With
statements can be nested, as long as the innerWith
statement refers to a child object or a dependent object of the outerWith
statement.
Programming Tips & Gotchas
It’s important that you do not include code within the
With
statement block that forces execution to
branch out of the block. Similarly, don’t write code that
forces program flow to jump into a With
block.
Both the With
and its associated
End
With
statement must be
executed or you will generate unpredictable errors and results.
See Also
Do . . . Loop Statement, Set Statement |
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.