0

I have the following code :

objIE.Document.All.a_l_1.click

But I want to do something like this :

objIE.Document.All. & some_var & .click
1
  • 1
    try objIE.Document.All[some-var].click Commented Jan 30, 2013 at 2:51

2 Answers 2

3

You can access the elements of the document.all collection by name, e.g.:

document.all("a_l_1").click

So there is no need for either Execute or Eval().

Update:

This .HTA:

<html>
 <!-- !! http://stackoverflow.com/questions/14595716/using-variables-in-commands-vbscript
 -->
 <head>
  <title>VariableDemo</title>
  <HTA:APPLICATION
    APPLICATIONNAME="VariableDemo"
  >
  <SCRIPT Language="VBScript">
   Sub AClick()
     Report document.all.bttA
     Dim sBtt
     For Each sBtt In Array("bttB", "bttC")
         document.all(sBtt).click
     Next
   End Sub
   Sub XClick(bttX)
     Report bttX
   End Sub
   Sub Report(bttX)
     document.all("txtA").innerText = bttX.innerText & ": " & Now() & vbCrLf & document.all("txtA").innerText
   End Sub
  </SCRIPT>
 </head>
  <body>
   <form>
    <button id="bttA" onclick="AClick">A</button>
    <button id="bttB" onclick="XClick Me">B</button>
    <button id="bttC" onclick="XClick Me">C</button>
    <br />
    <textarea id="txtA" rows="15" cols="40"></textarea>
   </form>
 </body>
</html>

'output'

demonstrates that

Report document.all.bttA                - access via named property

document.all("txtA").innerText = ...    - access via string literal

For Each sBtt In Array("bttB", "bttC")  - access via variable
    document.all(sBtt).click

all 'work', if the phase of the moon does not interfere.

Sign up to request clarification or add additional context in comments.

3 Comments

I can do that, but i want to use a variable instead of a string. But for some reason, it doesn't work.
@yuknow - please see update; you must provide more info to hunt down this strange reason.
I really appreciate the time you took with this answer. I should have been more clear with what I needed. I figured it out though, i just used this: dim isID | isID = 1 | do while isID < 5 | isID=isID + 1 | objIE.document.all("a_1_" & isID) | loop
1

Eval is what you want. It allows you to evaluate VBScript in string form, which then of course allows you to create that string however you wish. For example:

Dim myElement, evalResult
myElement = "a_l_1"
evalResult = Eval("objIE.Document.All." & myElement & ".click")

1 Comment

DO you mean something like this: Eval(objIE.Document.All.[clicker].click)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.