-1

I am new to programming and this is my first question. Thanks in advance for your support.

I have to download PDFs from a website constantly. When clicking on a button it opens a second tab and the PDF shows. The URL appearing on browser address bar is not exactly the URL of the PDF, it is just a generic website that is triggered through tokens to open the PDF.

Since I do not have the URL, I haven't been able to use URLDownloadToFile option. This option as well corrupts the PDFs and it hasn't been working for me. I was unable to use XMLHTTP since it requires a URL as well.

Unless you have other suggestions, I think clicking the Save button appearing on the frame of the PDF opened in IE could be the solution. I wound't like to use send keys since it is going to be a process running while the PC is locked. I already know how to manipulate the "Save As" window. I do not know how to invoke the Save button. Can some one show me how to do this?

![IE PDF Screen][This is the button I want to invoke]1

4

1 回答 1

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
于 2019-03-28T18:53:28.663 回答