VBA设置下拉值

时间:2015-08-17 14:21:57

标签: excel vba

我有一个用excel编写的宏,我使用" ActiveWorkbook.FollowHyperlink"导航到一个网页,它就像我需要的那样工作!

但是,我现在需要更新该网页上的下拉菜单。

我有一个下拉列表的ID,每个选项显然都有一个值。我想使用excel表中的值设置所选选项。

我很难挣扎,因为一旦使用.FollowHyperlink打开,我就不知道如何访问页面上的元素。

.FollowHyperlink是网页然后是活动的,是否有类似ActiveWebPage.getElementById的内容?

感谢任何帮助。

麦克

1 个答案:

答案 0 :(得分:0)

您要做的是使用com自动化来调用Internet Explorer的实例并导航到相关页面,这将为您提供文档模型,从那里您可以做任何事情,请参阅IE (Internet Explorer) Automation using Excel VBA < / p>

示例VBA如下

Private Sub IE_Autiomation()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = False

IE.Navigate "http://www.excely.com/"

' Statusbar
Application.StatusBar = "www.excely.com is loading. Please wait..."

' Wait while IE loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

' Find 2 input tags:
'   1. Text field
'   <input type="text" class="textfield" name="s" size="24" value="" />
'
'   2. Button
'   <input type="submit" class="button" value="" />

Application.StatusBar = "Search form submission. Please wait..."

Set objCollection = IE.document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).Name = "s" Then

        ' Set text for search
        objCollection(i).Value = "excel vba"

    Else
        If objCollection(i).Type = "submit" And _
           objCollection(i).Name = "" Then

            ' "Search" button is found
            Set objElement = objCollection(i)

        End If
    End If
    i = i + 1
Wend
objElement.Click    ' click button to search

' Wait while IE re-loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

' Show IE
IE.Visible = True

' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

Application.StatusBar = ""
End Sub