我做在VBA脚本一些IE的自动化,以及由于某种原因,为什么我的VBA脚本表现得如此难以预测?
“设置IE =的CreateObject(” InternetExplorer.Application“)”
行我的代码似乎是循环的,因为它会有时会启动3-7个IE浏览器而不是一个。最令人烦恼的部分是它在我的机器上工作正常,但是当我通过电子邮件发送给其他人时,它通常无法正常运行。这里是我的脚本:
Private Sub LauncherButton_Click()
'Here an instance of Internet Explorer is created,
' and pointed at the login page
Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
URL = "www.google.com"
IE.Visible = True
IE.Navigate URL
'The script waits for Internet Explorer to finish loading
Application.Wait Now + TimeValue("00:00:03")
'Here the login info is taken from the user inputs and entered
' login page
ID = Worksheets("home").Range("B25")
PW = Worksheets("home").Range("B26")
Application.SendKeys ID, True
Application.SendKeys "{TAB}", True
Application.SendKeys PW, True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
'This command will 'click' the login button, it will remain
' commented until a user with access can run the script
'Application.SendKeys "~", True
'After the login info is entered it is removed from memory
Worksheets("home").Range("B25").Clear
Worksheets("home").Range("B26").Clear
ID = ""
PW = ""
End Sub
我改变了网站,护目镜,因为实际的目标只是在我们的内部网络访问。 sendKeys部分也是不相关的,因为登录页面没有被加载。
我建议使用微软库,Internet控件和HTML来使用正确的对象,并且使用readystate和status而不是3秒循环。也使用对象而不是发送密钥,所以'getElementByID(“UserName”)。value = ID' –
谢谢,我正在研究它。一旦我做出这些更改,我将更新此帖子 –
是否有另一种获取元素的方法?该html代码没有id或标签属性。 –