简体   繁体   English

使用 VBA 从 Internet Explorer 窗口自动保存 PDF 文件

[英]Auto save PDF file from Internet Explorer window using VBA

I am using below code to automate saving the PDF document from Internet Explorer window.我正在使用以下代码从 Internet Explorer 窗口自动保存 PDF 文档。 It's working fine, but I want it to be happened for multiple PDF files with multiple URL's.它工作正常,但我希望它发生在具有多个 URL 的多个 PDF 文件中。 When I give URL's in column A and destination path with file format as .pdf in column B by taking URL from column A and save file with file name from column B.当我通过从 A 列中获取 URL 并使用 B 列中的文件名保存文件时,在 A 列中提供 URL 和在 B 列中提供文件格式为.pdf目标路径时。

Option Explicit 

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

Sub z() 

    Dim strSource As String 
    Dim strDest As String 
    strSource = "http://www.cran.r-project.org/doc/manuals/R-intro.pdf" 
    strDest = "c:\temp\blah.pdf" 
    URLDownloadToFile 0, strSource, strDest, 0, 0 

End Sub

As @shA.t was saying, all you need to do is to wrap those lines in a For Each loop.正如@shA.t 所说,您需要做的就是将这些行包装在For Each循环中。

Solution: Let's say, you have your URLs in A1 to A3 and your destinations in B1 to B3.解决方案:假设您的 URL 位于 A1 到 A3 中,而目的地位于 B1 到 B3。 Your Sub z() should look like this:您的Sub z()应如下所示:

For Each source in Sheets("Sheet name").Range("A1:A3")
    URLDownloadToFile 0, source.Value, source.Offset(0,1).Value, 0, 0 
Next source

Explanation: For Each loops through all cell elements in range A1 to A3.说明: For Each循环遍历范围 A1 到 A3 中的所有单元格元素。 In each round of the loop, source becomes that cell.在循环的每一轮中, source成为那个单元格。 Instead of hardcoding the source into your code, you can just refer to source.Value , the contents of the current cell in the loop.无需将源代码硬编码到您的代码中,您只需引用source.Value ,即循环中当前单元格的内容。 For the destination, you can use the .Offset method which references to a neighboring cell by its relative distance to the cell it is being called from.对于目的地,您可以使用.Offset方法,该方法通过相邻单元格与调用它的单元格的相对距离来引用相邻单元格。 In this case, we want to get from A1 to B1 (and so on), ie zero rows down, one column right ( Offset(0,1) ).在这种情况下,我们希望从 A1 到 B1(依此类推),即向下零行,向右一列( Offset(0,1) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VBA 从 Internet Explorer 下载文件 - How to download a file from internet explorer using VBA 使用Excel VBA从Internet Explorer提取数据集-如何与弹出的保存/打开/保存进行交互 - Using Excel VBA to pull datasets from internet explorer - how to interact with save/open/save as pop up VBA中的Internet Explorer子窗口 - Internet Explorer child window in VBA VBA 激活 Internet Explorer Window - VBA to Activate Internet Explorer Window VBA在下载xls文件时忽略了Internet Explorer的另存为弹出窗口 - VBA to omit save as popup from internet explorer while downloading xls file 如何通过VBA完成文件下载和Internet Explorer中的“另存为” - How to complete the file download and “save as” in Internet Explorer by VBA 使用 Excel VBA 从当前的 Internet Explorer 浏览器会话下载 PDF? - Download PDF from current Internet Explorer browser session with Excel VBA? VBA - 从 Internet Explorer 的框架通知栏中选择另存为 - VBA - choose save as from the frame notification bar of internet explorer VBA / Internet Explorer / Javascript:如何从同一Internet Explorer打开JavaScript窗口? - VBA / Internet Explorer / Javascript: How to open a javascript window from same internet explorer? 当使用VBA直接从Internet Explorer打开CSV文件时,我无法与该文件进行交互。 - When using VBA to open a CSV file directly from internet explorer, I cant then Interact with the file.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM