[英]Download file from network drive local server to a folder on click of a hyperlink in excel
I have an excel where there are around 30K hyperlinks pointing to a.network drive location where all the files are stored.我有一个 excel,其中有大约 30K 个超链接指向存储所有文件的网络驱动器位置。 I am trying to create a macro where when i click on the hyperlink it downloads the file to a folder like 'Downloads' where as right now it is just viewing the file in IE.
我正在尝试创建一个宏,当我单击超链接时,它会将文件下载到像“下载”这样的文件夹,而现在它只是在 IE 中查看文件。
Have not much idea on macros but have tried using shell app.browseforfolder, FileCopy and URLDownloadToFile but still getting errors.Also have tried using Selection to download selected cells hyperlinks but no dice.对宏不太了解,但尝试使用 shell app.browseforfolder、FileCopy 和 URLDownloadToFile 但仍然出现错误。还尝试使用 Selection 下载选定的单元格超链接但没有骰子。
Sample excel file is here: https://filebin.net/06n8hp1wm8y69oqw样本 excel 文件在这里: https://filebin.net/06n8hp1wm8y69oqw
Network drive links are like:网络驱动器链接如下:
\\10.111.11.30\Accounts\EP-D365\39156.jpg \\10.111.11.30\账户\EP-D365\39156.jpg
\\10.111.11.30\Accounts\EP-D365\39157.jpg \\10.111.11.30\账户\EP-D365\39157.jpg
\\10.111.11.30\Accounts\EP-D365\39158.msg \\10.111.11.30\账户\EP-D365\39158.msg
I managed to put together below code from online resources but doesnt work as it is for download from inte.net and not.network drive local server:我设法从在线资源中整理了以下代码,但无法正常工作,因为它是从 inte.net 和 not.network 驱动器本地服务器下载的:
Sub DownloadFile()
Dim WinHttpReq As Object
Dim oStream As Object
Dim myURL As String
Dim LocalFilePath As String
''For Each hlink In ThisWorkbook.Sheets("Main").Hyperlinks
myURL = "\\10.111.11.30\Accounts\EP-D365\39156.jpg"
LocalFilePath = "C:\Users"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "", ""
WinHttpReq.send
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile LocalFilePath, 2
oStream.Close
End If
''Next
End Sub
Any help appreciated.Thanks.任何帮助表示赞赏。谢谢。
You can use FileCopy
to copy files from a file system - works with UNC pathes.您可以使用
FileCopy
从文件系统复制文件 - 使用 UNC 路径。 I think your problem comes from the destination - Runtime error 75 is Path/File access error which usually means that the file cannot be written.我认为您的问题来自目的地 - 运行时错误 75 是路径/文件访问错误,这通常意味着无法写入文件。
I am not 100% sure about all details of FileCopy
, but I think you have to give the filename for the destination also - it's different than copy
on the command prompt.我不是 100% 确定
FileCopy
的所有细节,但我认为你还必须提供目标的文件名 - 它不同于命令提示符下的copy
。 If you just write the destination folder, you get the error 75 because FileCopy
tries to replace the destination folder with the file - which of course doesn't work.如果您只写入目标文件夹,则会收到错误 75,因为
FileCopy
试图用文件替换目标文件夹——这当然不起作用。
The following code will copy both files from Http and from file system.以下代码将从 Http 和文件系统复制这两个文件。 I did it a little quick&dirty by checking if the path contains a "Slash" or "Backslash" (Mac users will need to find a different attempt).
我通过检查路径是否包含“斜杠”或“反斜杠”(Mac 用户将需要找到不同的尝试)来快速和肮脏地完成它。 Filename is extracted from the source and glued to the dest folder.
文件名从源中提取并粘贴到目标文件夹。
A small advice: Don't hardcode the user path, you can use environ("userprofile")
instead.一个小建议:不要对用户路径进行硬编码,您可以改用
environ("userprofile")
。
And one remark: Code will fail if the destination folder does not exist.还有一点:如果目标文件夹不存在,代码将失败。
Sub test()
Const source = "\\10.111.11.30\Accounts\EP-D365\39156.jpg"
Const url = "https://i.stack.imgur.com/WyPLd.png"
CopyFile source, Environ("TEMP")
CopyFile url, Environ("TEMP")
End Sub
Sub CopyFile(source As String, destPath As String)
If InStr(source, "/") > 0 Then
DownloadFile source, Environ("TEMP")
Else
Dim filename As String, p As Integer
p = InStrRev(source, "\")
filename = Mid(source, p + 1)
FileCopy source, destPath & "\" & filename
End If
End Sub
Sub DownloadFile(url As String, destPath As String)
Dim WinHttpReq As Object
Dim oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", url, False, "", ""
WinHttpReq.send
Dim filename As String, p As Integer
p = InStrRev(url, "/")
filename = Mid(url, p + 1)
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile destPath & "\" & filename, 2
oStream.Close
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.