Excel VBA查找网页链接ID

时间:2013-04-30 18:12:27

标签: excel-vba vba excel

我正在使用的网页上有60到100个菜单链接,具体取决于用户凭据。我使用此代码点击菜单链接:

 Set oapp = CreateObject("Shell.Application")
         For i = 0 To 25
         StrName = ""
         On Error Resume Next
         StrName = oapp.Windows(i).Document.URL
         If InStr(StrName, "mywebaddress.aspx") Then
         Set oie = oapp.Windows(i)
         Exit For
         End If
         Next
         oie.Document.all("ctl11_repeaterNavigator_ctl32_menuLink").Click

但链接号会根据用户凭据而更改 - 我的凭据_ctl32可以是_ctl32或_ctl33。有没有办法搜索内部文本并将链接ID存储为字符串?

I know I can click on the link by using:
For i = 1 To oie.Document.all.Length
 If oie.Document.all.Item(i).innerText = "Letters" Then
 oie.Document.all.Item(i).Click
 Exit For
End If
Next i

但我需要在字符串中使用链接ID存储以进行下一步

1 个答案:

答案 0 :(得分:0)

也许尝试类似这样的事情,假设链接的Name是“Letters”,并且假设前缀“ctl11_repeaterNavigator_”和足够的“_menuLink”对用户不会改变。

Dim ele as Variant
Dim parseString as String

'## Determine what ctl to use:'

For each ele in oie.Document.GetElementsByName("Letters") '## Modify to use the Name associated with this menu link '
    If Right(ele.InnerHTML,9) = "_menuLink" AND _
        Left(ele.InnerHTML, 24) = "ctl11_repeaterNavigator_" Then 
        ele.Click
        Exit For
    End If
Next

可能必须使用ele.InnerText而不是.InnerHTML,所以如果上述方法不起作用,请尝试稍微调整一下。