With - VBScript
Overview
With creates a contextual scope for setting and getting properties and methods of an object. It simplifies and reduces the length of code, especially when working with nested objects.
Syntax
With object
[property OR method]
...
End With
Options/Flags
None.
Examples
Example 1: Simple Object Manipulation
With MyRange
.Value = "Hello World"
.Font.Size = 12
End With
Example 2: Nested Objects
With MyDocument.Sheets("Sheet1")
.Range("A1") = "Value"
.Rows("2").Delete
End With
Common Issues
- Incorrect object reference: Ensure the provided object exists before using With.
- Missing End With: Always follow With with End With to terminate the contextual scope.
Integration
With is often combined with other commands to manipulate objects efficiently:
- For Each to iterate through a range of objects.
- Set to assign a new object to the current object reference.
- Events to handle object events within a specific scope.
Related Commands
- New
- Set
- Object