无法在Excel VBA中停止“Do Until”循环

时间:2017-08-18 15:07:26

标签: excel vba excel-vba

所以我的新问题是我无法获得以下代码来停止循环。看起来好像我的“直到元素无关”这一行可能不完全准确,因为我试图点击的按钮在运行结束时没有显示“无”值。以下是代码,简要说明了该问题。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="results_table" style="display: inline">Results Table</section>
<section class="results_charts" style="display: none">Results Charts</section>

<div class="btn-group">
    <button type="button" id="table" class="btn btn-primary">Table</button>
    <button type="button" id="chart" class="btn btn-primary">Charts</button>
</div>


这是按钮可见时的html读数

Sub SearchBot()


Dim objIE As InternetExplorer
Dim y As Integer
Dim result As String

Set objIE = New InternetExplorer

objIE.Visible = True

objIE.navigate "https://www.searchiqs.com/nyalb/Login.aspx"

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

objIE.document.getElementById("btnGuestLogin").Click

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

objIE.document.getElementById("ContentPlaceHolder1_txtFromDate").Value = "07/11/2017"

objIE.document.getElementById("ContentPlaceHolder1_txtThruDate").Value = "07/13/2017"

objIE.document.getElementById("ContentPlaceHolder1_cboDocGroup").Value = "DBA"

objIE.document.getElementById("ContentPlaceHolder1_cmdSearch").Click

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

y = 1

' brings up a table of results. I need to click on the view button and get the info from that page

objIE.document.getElementById("ContentPlaceHolder1_grdResults_btnView_0").Click


Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

' element ID's are hidden for those looking to help

result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)

Sheets("Sheet1").Range("A" & y).Value = result

'Sheets("Sheet1").Range("C" & y).Value = result

y = y + 1


' I would love to direct link to the next search result here but the link does not change between pages so I click the next button

objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

 ' I believe this is where the issue occurs

Do Until objIE.document.getElementById("ContentPlaceHolder1_btnNext") Is Nothing

result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)

Sheets("Sheet1").Range("A" & y).Value = result

y = y + 1

' when the next page button is visible, it goes to the next page. When it is dimmed, the code continues. It doesn't stop unless I manually stop it. It takes the final page and repeats the process infinitely. 

objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

Loop


' I end up with a few good results in the beginning and then the final result repeats until I eventually stop the script.

End Sub

这是按钮变暗时的html读数

<input name="ctl00$ContentPlaceHolder1$btnNext" title="Next Document" id="ContentPlaceHolder1_btnNext" style="width: 35px;" onclick="SaveDocListSelected();" type="submit" value=">>">

感谢任何帮助。

0 个答案:

没有答案