如何使用excel vba从Web浏览器下载pdf.php文件

时间:2018-03-05 14:31:19

标签: excel vba excel-vba

您好我正在尝试从网络浏览器下载名为“view.pdf.php文件的文件并将其另存为pdf文件。(在网络浏览器中点击打印图标时获取文件,它会打开.pdf.php文件在另一个浏览器中)

尝试了堆栈溢出的一些样本,但无法以正确的格式下载。

代码尝试了:

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
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
Sub Test()
DownloadFile "https://app.goscripts.com/vendor/viewreferral.php?Id=XXXXX", "\\test\test.pdf"

End Sub

当试图打开下载的文件时,它显示为“Adobe Acrobat Reader无法打开'test.pdf',因为它不是受支持的文件类型,或者因为文件已损坏” 请帮我解决这个错误。

1 个答案:

答案 0 :(得分:1)

找到另一种从网络浏览器保存文件的方法,
代码:

Const OLECMDEXECOPT_DODEFAULT = 0
Const OLECMDEXECOPT_PROMPTUSER = 1
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Const OLECMDEXECOPT_SHOWHELP = 3
Const OLECMDID_SAVEAS = 4
Const PRINT_WAITFORCOMPLETION = 2


Sub Sample()
Dim objIE, sh As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "https://123.com/vendor/vieworder.pdf.php?OrderId=XXXX"
objIE.Visible = True
Do While objIE.ReadyState <> 4
DoEvents
Loop
objIE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
Set sh = CreateObject("WScript.Shell")
sh.AppActivate "Save AS"
Application.Wait (Now + TimeValue("00:00:02"))
sh.SendKeys "done", 1
sh.SendKeys ("{ENTER}")
objIE.Quit
End Sub

感谢您的支持