简体   繁体   English

VB单击按钮宏注意工作

[英]VB Click Button Macro Note Working

I am trying to get the results of tracking a package on my screen. 我正在尝试获取在屏幕上跟踪包的结果。 In Excel, I have this code, which successfully opens the IE browser and successfully types the tracking number into the input box. 在Excel中,我有这个代码,它成功打开IE浏览器并成功地将跟踪号输入到输入框中。 What doesn't work is the last step - button click. 什么不起作用是最后一步 - 按钮单击。 I get the runtime error 91 - object variable or with block variable not set. 我得到运行时错误91 - 对象变量或未设置块变量。 Thanks in advance. 提前致谢。

Sub TrackUSPS() Sub TrackUSPS()

Const cURL = "https://tools.usps.com/go/TrackConfirmAction!input.action" 'Enter the web address here
Const trackNum = "9405511899560005266920" 'Tracking number 

Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim TrackInputBox As HTMLInputElement
Dim TrackButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate cURL

'Wait for initial page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

Set doc = IE.document

'Get the tracking form on the page

Set LoginForm = doc.forms(1)

'Get the tracking input box and populate it
'<input type="text" id="search-text" autocomplete="off" name="searchText" class="default" value="Search USPS.com or Track Packages" onclick="javascript:dojo.byId('search-text').value='';"/>

Set TrackInputBox = LoginForm.elements("search-text")
TrackInputBox.Value = trackNum

'Get the form input button and click it
'<input type="image" id="search-btn" src="/media/images/global/blank.gif" alt="Process Search"  />

Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click

'Wait for the new page to load

Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

End Sub

This works for me, replace Set TrackButton = LoginForm.elements("search-btn") 这对我有用,取代Set TrackButton = LoginForm.elements(“search-btn”)
TrackButton.Click TrackButton.Click

with
IE.document.getelementbyID("search-btn").Click IE.document.getelementbyID( “搜索BTN”)。点击

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

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