[英]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.