首页 文章

需要使用Excel VBA下拉菜单自动化IE

提问于
浏览
0

我想自动化一个名为“Timesheet”的下拉菜单的站点,然后单击列表中第3个菜单项“Project” .

这是HTML代码:

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" ng-click="$event.preventDefault()" title="Timesheet" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-time"></span> Timesheet <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a ui-sref="AddTime" title="Add time" href="/Timesheet/AddTime/">Add time</a></li>
                            <li><a ui-sref="ApproveTime" title="Approve time" href="/Timesheet/ApproveTime">Approve time</a></li>
                            <li><a ui-sref="Projects" title="Projects" href="/Timesheet/Admin/Projects">Projects</a></li>
                            <li><a ui-sref="BudgetCode" title="Budget Code" href="/Timesheet/Admin/BudgetCode">Budget Code</a></li>
                            <li><a ui-sref="WorkCode" title="Work Code" href="/Timesheet/Admin/WorkCode">Work Code</a></li>
                            <li><a ui-sref="Functions" title="Functions" href="/Timesheet/Admin/Functions">Functions</a></li>
                            <li><a ui-sref="WorkStreams" title="Work Streams" href="/Timesheet/Admin/WorkStreams">Work Streams</a></li>
                            <li><a ui-sref="EmployeeResource" title="Employees" href="/Timesheet/Admin/EmployeeResource">Employees</a></li>
                        </ul>
                    </li>
                </ul> 
            </div>

VBA代码:Private Sub IE_Test()Dim i As Long Dim IE As Object Dim ElementCol As Object

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

'IE.Visible = False

IE.Navigate "http://st-toss/"

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

IE.Visible = True

Set ElementCol = IE.Document.getElementsByClassName("dropdown-menu")

ElementCol.Item(2).Click

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

Application.StatusBar = ""

结束子

我尝试通过classname / tagname进行选择,但是一旦我没有添加任何断点,我就得到了相同的错误自动化错误/未指定的错误 . 一旦我把断点放在get方法错误消息上,就说调用的对象与客户端断开连接 .

请建议我如何通过这个 .

Refrences for VBA

1 回答

  • 0

    ElementCol是“dropdown-menu”类中元素的集合 . (您在设置getElementsByClassName时创建了此集合 . 请注意,“Elements”是复数 . )

    该课程中有多少元素,哪一个是您需要的元素?
    如果这是该类中唯一的元素,那么您可以通过以下方式引用它:

    elementCol(0).selectedIndex = 2
    

    或类似的东西 .

相关问题