使用VBA时处理IE中的打开/保存/取消对话窗口

时间:2014-06-29 01:07:25

标签: vba internet-explorer download dialog

首先,感谢您花时间和兴趣研究这个问题。我一直在使用VBA在Excel中自动执行手动任务,但最近才开始探索使用VBA访问Web。

目标:从网站中自动执行文件下载(每天约15-20个xlsx文件),其中文件网址无法在页面的源代码中找到。

以下是我手动下载这些步骤时通常采取的步骤。

  1. 打开登录页面并输入登录凭证以访问感兴趣的网页(即包含所有报告的网页)
  2. 登录后,导航到包含该报告的网页 note1:设置为1个网页(唯一网址)=在第一页显示前55个结果
  3. note2:同一页面还有一个按钮,可以用不同的格式导出/保存整个报告

    1. 下载报告

    2. 导航到下一个网页(在同一网站内)并重复步骤2和3(大约有15-20个报告/网页导航)

    3. 通过使用SendKeys点击保存,我已经下载了第一份报告。虽然有时它会在对话窗口出现后立即停止,但这已经达到了最远的程度。在此之后,我无法导航到另一个网页并重复相同的步骤进行下载。我的直觉是单击保存按钮后出现的打开/打开文件/视图下载对话窗口不允许我重复下载/保存过程......

      我试着查看网站的源代码,看看我是否能找到该文件的网址,但找不到它(不确定是否必须这样做,导出只在点击提交按钮后才会发生隐藏文件URL或其他类似运行脚本的内容。我对WinHttpRequest不是很熟悉,但在进行谷歌研究后似乎是首选的方法。看起来这也需要有一个文件URL,但不确定这个......

      以下是我到目前为止汇总的代码。任何帮助将非常感谢。谢谢! :)

      Sub webMacro()
      
      Dim IE As New InternetExplorer
          IE.Visible = True   'change False --> True to open the IE window
          IE.navigate "https://websiteURL.net//apps/login.aspx"
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      Dim Doc As HTMLDocument: Set Doc = IE.document
      
      Doc.getElementById("username").Value = "myusername"  'login to the website
      Doc.getElementById("pass").Value = "mypassword"
      Doc.getElementById("Enter").Click
      
      
      Sleep (1000)
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      IE.navigate "https://firstReportWebPage.net//apps/....."        'navigates to the first webpage (report) to download after login
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      Doc.getElementById("##########").Click     'ID of the Export/Save button
      
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      
      Doc.getElementById("###########").Click     'ID of the Submit button
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      
      Doc.getElementById("############").Click        'ID of the field right before it enters the Open/Save/Cancel dialogue window
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      
      Application.Wait (Now + TimeValue("0:00:02"))  'here I'm using the SendKeys to mimic what I would manually do on the keyboard to get to the "Save" button
         SendKeys "{TAB}", True
         SendKeys "{TAB}", True
         SendKeys "{DOWN}", True
         SendKeys "{ENTER}", True
      
      Sleep (1000)
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      Sleep (1000)
      
      
      ''***This is where it almost always gets stuck...here I'm attempting to get to the Open/Open file/View downloads dialogue window by clicking on the field right before entering the dialogue window using the tab key; same as above when trying to click on the "Save" button in the Open/Save/Cancel dialogue window.
      
      Doc.getElementById("############").Click        'ID of the field right before it enters the Open/Open File/View Downloads dialogue window
      
      Sleep (1000)
      
      Do
          DoEvents
      Loop Until IE.readyState = READYSTATE_COMPLETE
      
      Sleep (1000)
      
      Application.Wait (Now + TimeValue("0:00:02"))
      
      Sleep (1000)
         SendKeys "{TAB}", True
      Sleep (1000)
         SendKeys "{TAB}", True
      Sleep (1000)
         SendKeys "{TAB}", True
      Sleep (1000)
         SendKeys "{TAB}", True
      Sleep (1000)
         SendKeys "{ENTER}", True
      Sleep (1000)
      
      
      'some other code to go here...
      
      End Sub
      

1 个答案:

答案 0 :(得分:0)

我一直看到'不要使用sendkeys'其他人建议,但当我尝试做类似的事情时,我并不真正知道他们的意思。

SendKeys有时会随机复制密钥发送(我用它来同时控制16个窗口),每个窗口有1组指令和18,000条必须处理的指令。

对于浏览器解析的每500条指令,它发生了大约2-3次,我无法找到解决方法。

在浏览网站时,我写了一些内容,然后我也写了下载页面HTML的内容。

您是否可以使用“打开/保存/取消”对话框下载页面的HTML源代码,并查看该按钮等页面上是否存在该文件的URL?

如果是这样,您可以自动导航到该页面,然后下载HTML(如果网址在源中,我可以拥有代码),然后在VBA中解析HTML以计算下载URL?< / p>