使用Excel VBA创建文件夹并从weblink上传图像?

时间:2013-03-05 14:48:55

标签: excel vba

我正在寻找使用excel为大约12000个条目创建文件夹系统的方法,MkDir功能似乎很容易,但我仍然坚持如何让其余的工作。任何建议都会有所帮助

我在想什么:

IndexID | Image
1       | sampleimage.com/images.jpg
2       | sampleimage.com/image.jpg
3       | sampleimage.com/moreimages.jpg
3       | sampleimage.com/evenmoreimages.jpg

将变成这个文件结构:

%dir%\Images\1\images.jpg
%dir%\Images\2\image.jpg
%dir%\Images\3\moreimages.jpg
%dir%\Images\3\evenmoreimages.jpg

或者这个:

%dir%\Images\1\1.jpg
%dir%\Images\2\1.jpg
%dir%\Images\3\1.jpg
%dir%\Images\3\2.jpg

如果这些文件尚未下载并保存到相应的文件夹,我想使用VBA下载这些文件

1 个答案:

答案 0 :(得分:2)

这可能会对您有所帮助:

http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/beb6fa0e-fbc8-49df-9f2e-30f85d941fad/

它们展示了如何使用API​​从Web下载文件。用于设置目录结构并将其全部放在一起(此代码尚未运行...您必须添加API声明):

Option Explicit

' api declarations go here
' ...


Const directory As String = "C:\Images"

Sub BacardiAndColaDoItDoIt()
    Dim sh As Excel.Worksheet
    Dim i As Long
    Dim iLastRow As Long
    Dim theFolderPath As String, theFilePath As String
    Dim fileName As String

    Set sh = ThisWorkbook.Worksheets("Sheet1")

    iLastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row

    ' create first directory
    On Error Resume Next
    MkDir directory
    On Error GoTo 0

    For i = 2 To iLastRow
        theFolderPath = directory & "\" & sh.Cells(i, 1).Value
        fileName = Mid(sh.Cells(i, 2).Value, InStrRev(sh.Cells(i, 2).Value, "/") + 1, Len(sh.Cells(i, 2).Value))
        theFilePath = theFolderPath & "\" & fileName

        ' create subdirectory
        On Error Resume Next
        MkDir theFolderPath
        On Error GoTo 0

        'DownloadFile(sh.Cells(i,2).value, theFilePath)
    Next i
End Sub
相关问题