Retrieving Other Object Variables

I'm trying to retrive variable values from each object on my desktop by means of

"For each obj in DesktopX.Objects
MsgBox DesktopX.ScriptObject(obj.Name).objGroupNumber
Next"

The problem I have is that not all of my objects have "objGroupNumber" instantiated. When the for loop reaches an object without "objGroupNumber" instantiated it sends an "Object Required" error. Is there a way to check for object variables such as "IsObject" ?
7,972 views 6 replies
Reply #1 Top
Is there a way to check for object variables such as "IsObject" ?


Not sure, but you can stick On Error Resume Next before your code so it will ignore the error and keep going.


On Error Resume Next
For Each obj In DesktopX.Objects
MsgBox DesktopX.ScriptObject(obj.Name).objGroupNumber
Next
Reply #2 Top
Tried that. The Script continues execution but it causes another function in the script to fail when it resumes.
Reply #3 Top
Without knowing more about the variable/script that you've provided it's hard to give complete answers. My thinking is that maybe an If Statement within the For Next statement may be all you need?

Code: vbscript
  1. For each obj in DesktopX.Objects
  2. If DesktopX.ScriptObject(obj.Name).objGroupNumber > 1 And DesktopX.ScriptObject(obj.Name).objGroupNumber < 30 Then
  3. MsgBox DesktopX.ScriptObject(obj.Name).objGroupNumber
  4. Else
  5. End If
  6. Next



If all your items aren't in the same group why not create some custom states or what I do is cheat & use the comment field as a sort of temp storage field.

The reason I do this is to reduce the number of open variables running in the script, in order to streamline a larger script.
Reply #4 Top
SirSmiley, did you miss the part about the variable not being SET? If I call "DesktopX.ScriptObject(obj.Name).objGroupNumber" without "objGroupNumber" being set on the object "obj.Name", it will give an "Object Required" error AKA no var.

What I'm doing is emulating a virtual desktop by hiding all of the objects on my desktop except one. The visible one is a triger for the rest of my objects. Each time I hit the trigger I want the trigger object to:
1) Hide all object that aren't hidden.
2) Search each object for "objGroupNumber" var. and based upon what number is set show the objects with the same number. Objects that dont have this var, "objGroupNumber", instantiated I want to remain hidden.

Reply #5 Top
Okay. You are always going to get errors if you are searching each scripted object for a variable that isn't set. So, the only quick idea based on what you've given would be to set a zero-length string(or a null value) in all scripted objects then do the If Statement.

"Dim objGroupNumber
objGroupNumber=""

Code: vbscript
  1. For each obj in DesktopX.Objects
  2. If DesktopX.ScriptObject(obj.Name).objGroupNumber="" Then
  3. Else
  4. MsgBox DesktopX.ScriptObject(obj.Name).objGroupNumber
  5. End If
  6. Next
Reply #6 Top
Ignore what I just said? This script worked for me.

Code: vbscript
  1. For Each obj In DesktopX.Objects
  2. On Error Resume Next
  3. 'MsgBox ("Error # " & CStr(Err.Number) & " " & obj.Name)
  4. 'Err.Clear ' Clear the error.
  5. With DesktopX.ScriptObject(obj.Name)
  6. If .objGroupNumber=1 Then
  7. DesktopX.Object(obj.Name).Visible = False
  8. MsgBox .objGroupNumber
  9. End If
  10. End With