Question:
What is VBScript’s Execute function for? How can I use it?
Solution:
VBScript’s Execute function interprets a string as a VBScript instruction and executes it in the current context. It is useful for creating scripts that check or attribute values of properties with sequential names. For example, values are usually attributed in properties like this:
Application.GetObject("Data.XObject1").Property1 = 0
However, when you need to update values in a large group of properties, you can use Execute function. The two examples below will perform the exact same action, but you will be able to better understand the difference between using a simple attribution and an attribution via Execute method.
Simple attribution
Application.GetObject("Data.XObject1").Property1 = 1
Application.GetObject("Data.XObject1").Property2 = 1
Application.GetObject("Data.XObject1").Property3 = 0
...
Application.GetObject("Data.XObject1").Property200 = 0
Attribution using Execute function
Set obj = Application.GetObject("Data.XObject1")
For i=1 to 200
Execute "obj.Property" & CStr(i) & " = 1 "
Next
Notice that scripts using Execute function are usually smaller, which makes maintaining/debugging them easier.
The sample application attached to this article shows Execute function changing the values of properties in an XObject.