我有此链接: https://s23527.pcdn.co/wp-content/uploads/2017/04/wine_speedlights_kit_lens.jpg.optimal.jpg
(我不介意如何获取它,在单元格B2
上可以是1920,在单元格C2
上可以是1080)
答案 0 :(得分:5)
您需要对URLDownloadToFile
进行API调用才能下载图像。在下面的示例中,我们将下载到临时文件夹C:\Temp\
。
下载图像后,您将创建一个新的Shell对象,并最终使用.ExtendedProperty()
属性来获取文件尺寸
下载完文件后,可以继续使用Kill()
删除临时文件。
以下方法使用早期绑定。您将需要设置对
的引用Microsoft Shell Controls And Automation
通过在VBE菜单中转到
Tools -> References
Option Explicit
#If VBA7 Then
Declare PtrSafe 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
#Else
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
#End If
Sub test()
Const tmpDir$ = "C:\Temp\"
Const tmpFile$ = "tmpPicFile.jpg"
Debug.Print URLDownloadToFile(0, ActiveSheet.Range("A2").Value, tmpDir & tmpFile, 0, 0)
ActiveSheet.Range("B2").Value = getFileDimensions(tmpDir, tmpFile)
Kill tmpDir & tmpFile
End Sub
Private Function getFileDimensions(filePath$, fileName$) As String
With New Shell32.Shell
With .Namespace(filePath).ParseName(fileName)
getFileDimensions = .ExtendedProperty("Dimensions")
End With
End With
End Function