简体   繁体   English

需要使用Excel VBA通​​过下拉菜单自动执行IE

[英]Need to automate IE with drop down menu using Excel VBA

I want to automate a site having drop down menu named 'Timesheet' and then click on the Menu item 'Project' which is 3rd in the list. 我要自动化一个站点,该站点具有一个名为“时间表”的下拉菜单,然后单击菜单项“项目”,该菜单项在列表中位于第3位。

Here is the HTML code: 这是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 code: Private Sub IE_Test() Dim i As Long Dim IE As Object Dim ElementCol As Object VBA代码:私有Sub IE_Test()昏暗i作为长暗IE作为对象暗ElementCol作为对象

' 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 = ""

End Sub 结束子

I have tried selecting by classname/tagname but I have been getting the same error Automation error/Unspecified error once i did not add any break point. 我尝试按类名/标记名进行选择,但是一旦我未添加任何断点,就会遇到相同的错误自动化错误/未指定错误。 And once i put the breakpoint on the get method error message says object invoked disconnected from client. 一旦我将断点放在get方法上,错误消息就会说调用的对象与客户端断开了连接。

Please suggest how can i get through this. 请提出我该如何解决。

VBA的参考

ElementCol is a collection of elements in the class "dropdown-menu". ElementCol是“下拉菜单”类中的元素的集合。 (You created this collection when you set getElementsByClassName. Notice that "Elements" is plural.) (您在设置getElementsByClassName时创建了此集合。请注意,“ Elements”是复数。)

How many elements are there in that class, and which one is the one you need? 该类中有多少个元素,您需要哪一个?
If this is the only element in that class, then you would refer to it by : 如果这是该类中的唯一元素,则可以通过以下方式引用它:

elementCol(0).selectedIndex = 2

or something similar. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM