从IE宏下载文件丢失或跳过

时间:2019-09-17 04:52:06

标签: excel vba

我正在使用以下代码从网站下载文件。尽管它没有给出任何错误,但它丢失了一些文件(所有文件都标记为已下载)。 我在Excel工作表中有标准,该工作表放在网站的字段中,并且文件已下载(在excel中标记为已下载)。

Dim time1, time2
Dim ie As Object

        CreateObject("Shell.Application").Windows
          Set ie = CreateObject("InternetExplorer.Application")
          ie.Visible = True
          ie.Navigate "websitelink"
          While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
          ie.Document.getElementById("txtLogin").Value = "username"
          ie.Document.getElementById("txtPassword").Value = "password"
          ie.Document.getElementById("imgbtnLogin").Click
          While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
          ie.Document.getElementById("ctl00_LeftMenu1_lstLeftMenuList_ctl02_lnkItem").Click
          While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
          ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaMainUc1_dgSubject_ctl02_btnProcess").Click
          While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
          ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaDetailUc1_dgReporting_ctl04_lnkbtnReport").Click
          While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend

ie.Visible = True
Set objshell = CreateObject("Shell.Application")
iecount = objshell.Windows.Count
NROW = 2
ROW_FLAG = Sheet1.Cells(NROW, 1).Value

Do While ROW_FLAG <> ""
        ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaDetailUc1_MISEFEWiseData_txtExportNTN").Value = Sheet1.Cells(NROW, 1).Value
        ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaDetailUc1_MISEFEWiseData_txtDateFrom_txtDate").innerText = Sheet1.Cells(2, 5)
        ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaDetailUc1_MISEFEWiseData_txtDateTo_txtDate").innerText = Sheet1.Cells(3, 5)
        ie.Document.getElementById("ctl00_ContentPlaceHolder2_SubjectAreaDetailUc1_MISEFEWiseData_btnReport").Click
        Sheet1.Cells(NROW, 3).Value = "Downloaded"
        While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend

'time1 = Now
'time2 = Now + TimeValue("0:01:00")
'    Do Until time1 >= time2
'        DoEvents
'        time1 = Now()
'    Loop
    NROW = NROW + 1
    ROW_FLAG = Sheet1.Cells(NROW, 1).Value
    Do While iecount <> xx
    xx = objshell.Windows.Count
    Loop
Loop

    While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend

      Set ie = Nothing
MsgBox Sheet1.Cells(1, 1) & Sheet1.Cells(1, 5)
End Sub```

1 个答案:

答案 0 :(得分:0)

我猜单击按钮时.ReadyState.Busy属性保持不变。如果是这样,您实际上并没有在等待完成此操作,这就是为什么它缺少某些文件(第一个文件除外)的原因。到那儿去做吧。

您需要找到一种等待方式,直到可以下载下一个文件。我看到2个选项(硬编码延迟除外):

  1. 如果网站告诉您文件正在下载,请在特定元素更新时循环播放(例如,可能有一个正在加载的图标或显示“正在下载”的文本),什么?如果不确定,请在单击和比较之前和之后获取“ body” innerText
  2. 点击之前先对下载文件夹中的文件进行计数,然后等待点击后计数增加-是的,这并不理想,但这是一个选择

还要指出:

  • 我不确定Do While iecount <> xx的作用,但我会摆脱它,因为它可能非常不准确
  • 与其继续复制While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend而不是使其成为一个函数,而是使您的生活更轻松,并使代码更整洁-您可以添加可选参数,该参数必须出现在正文的innerText中(如上所述)
  • 您没有将IE设置为任何值,但是浏览器保持打开状态,除非需要您可以先添加IE.quit