With - VBScript


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.


With object
   [property OR method]
End With




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"
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.


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.
  • New
  • Set
  • Object