excel中的VBA如何将http://blahblah.pdf网页保存为pdf文件?

时间:2015-08-12 21:27:42

标签: excel vba excel-vba pdf save

目前我的宏按照特定链接浏览IE浏览器,并让我点击pdf链接并在IE中打开它。

我现在要做的是将该打开页面保存为pdf文件。我试过了URLDownloadtofile来保存文件但不允许我打开它。

以下是我的代码结尾的示例:

Loop
For Each e In ie.document.getElementsByTagName("span")
If e.InnerText = "View Assigned Workout" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:1"))

For Each e In ie.document.getElementsByTagName("strong")
If e.InnerText = "View/Print Workout" Then
e.ParentElement.Click
Exit For
End If
Next

Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
For Each e In ie.document.getElementsByTagName("strong")
If e.InnerText = "View Workout Card" Then
e.ParentElement.Click
Exit For
End If
Next
Application.Wait (Now + TimeValue("0:00:1"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

For Each e In ie.document.getElementsByTagName("strong")
If e.InnerText = "Print" Then
e.ParentElement.Click
Exit For
End If
Next
End Sub

此时单击打印链接在IE中打开文件 http://blahblah.pdf(无法登录实际链接)

如何将此文件作为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 Test()
Dim strPDFLink As String
Dim strPDFFile As String
Dim Result As Boolean
strPDFLink =  "http://blahblah.pdf"
strPDFFile = "savetestworkout.pdf"
Result = DownloadFile(strPDFLink, strPDFFile)
End Sub

Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

文件已创建,但是当我尝试打开它时会发生错误。 任何帮助将不胜感激。

0 个答案:

没有答案