VBA-如何在IE11中打开的PDF上调用“保存”按钮(无Sendkeys)

时间:2019-03-28 17:18:39

标签: excel vba pdf internet-explorer

我是编程新手,这是我的第一个问题。预先感谢您的支持。

我必须不断从网站上下载PDF。单击按钮时,它将打开第二个选项卡,并显示PDF。浏览器地址栏上显示的URL并不完全是PDF的URL,它只是通过令牌触发以打开PDF的通用网站。

由于我没有URL,因此无法使用URLDownloadToFile选项。此选项也会损坏PDF,并且对我没有用。我无法使用XMLHTTP,因为它也需要URL。

除非您有其他建议,否则我认为单击IE中打开的PDF框架上显示的“保存”按钮可能是解决方案。我不想使用发送密钥,因为它将是PC锁定时正在运行的进程。我已经知道如何操纵“另存为”窗口。我不知道如何调用“保存”按钮。有人可以告诉我该怎么做吗?

![IE PDF屏幕] [这是我要调用的按钮] 1

1 个答案:

答案 0 :(得分:0)

检查加载PDF的网站的代码,很有可能PDF URL可能在其中,如果可以的话,您可以使用VBA进入该站点并从代码中获取URL,获取URL后,您可以尝试使用URLmon库,该库允许您基于URL下载文件/网站。下面是要下载的代码,您提到您是编程新手,您将需要寻找代码来打开网站,导航到该网站并在html代码中搜索URL。 (同样,在开始构建此代码之前,请首先手动检查HTML,以确认pdf URL在其中。)

Option Explicit 
Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _ 
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _ 
ByVal lpfnCB As Long) As Long 

Public Sub DownloadPDF()
    getFile "http://www.website.com/path/file.pdf", "C:\Downloads\filename.pfd"
    if getFile = True Then MsgBox "Download success"
End Sub

Function getFile(URL As String, LocalFilename As String) As Boolean 
    Dim downloadCheck As Long
    'We need a long variable because the library returns a number if the file downloaded or if it failed.
    downloadCheck = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
    'Check if the number is 0, if so, the file downloaded fine.
    If downloadCheck = 0 Then getFile = True 
End Function