Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将文件下载到不同的路径_Vba_Web Scraping - Fatal编程技术网

Vba 将文件下载到不同的路径

Vba 将文件下载到不同的路径,vba,web-scraping,Vba,Web Scraping,请帮忙。我有一个代码来处理 使用工作表中提供的URL导航到internet explorer。-纵队 使用工作表中提供的名称为文件指定名称。-B柱 这将把下载的文件保存到excel工作表中提到的特定文件夹路径。-C柱 我有一个宏,可以做任何事情,但我正在努力工作的第三步。你能帮我一下吗 我的问题:我想将文件下载到不断变化的特定的文件夹中。 示例:一些ABC文件应下载到XYZ文件夹 一些CDE文件应下载到123文件夹 已创建多个具有不同名称的文件夹。。。意味着我需要根据需要动态更改文件夹路径 以下

请帮忙。我有一个代码来处理

  • 使用工作表中提供的URL导航到internet explorer。-纵队
  • 使用工作表中提供的名称为文件指定名称。-B柱
  • 这将把下载的文件保存到excel工作表中提到的特定文件夹路径。-C柱
  • 我有一个宏,可以做任何事情,但我正在努力工作的第三步。你能帮我一下吗

    我的问题:我想将文件下载到不断变化的特定的文件夹中。 示例:一些ABC文件应下载到XYZ文件夹 一些CDE文件应下载到123文件夹 已创建多个具有不同名称的文件夹。。。意味着我需要根据需要动态更改文件夹路径

    以下是我的VBA宏,供您参考:

    #If VBA7 And Win64 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal 
    pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, 
    ByVal lpfnCB As LongPtr) As Long
    #Else
    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
    #End If
    Dim Ret As LongPtr
    
    Sub DownloadFile()
    pth = "C:\VBA\"
    For Each link In ActiveSheet.Hyperlinks
    fname = Split(link.Address, "/")(UBound(Split(link.Address, "/")))
        Filename = pth & fname
        URLDownloadToFile 0, link.Address, Filename, 0, 0
    Next link
    End 
    End Sub
    

    谢谢…

    这是您的问题代码,已修改以获得所需的输出

    这是假设您正在工作表中输入值

    Sub DownloadFile()
    
    Dim Pth as String
    Dim Link As Variant
    Dim fname As String
    
    For Each Link In ActiveSheet.Hyperlinks
    fname = Split(Link.Address, "/")(UBound(Split(Link.Address, "/")))
        Pth = "C:\VBA\" & Link.Parent.Offset(0, 3).Value & "\"    'Finds the value of the cell 3 columns to the right of the hyperlink
    
        'The next 3 lines will create the path you specified in your "pth" variable _
        ' or ignore the error if it already exists - this will ensure you don't get errors _
        ' when saving the file to your folder (incase it hasn't been created for example).
        On Error Resume Next
            MkDir (Pth)
        On Error GoTo 0
    
        Filename = Pth & fname
        URLDownloadToFile 0, Link.Address, Filename, 0, 0
    Next Link
    End
    End Sub
    
    为未来读者澄清这些变化


    Pth
    分配为
    Pth=“C:\VBA\”&Link.Parent.Offset(0,3).Value&“\”
    For…Next
    循环中,允许我们根据
    列D
    值动态选择文件夹
    Link.Parent.Offset(0,3).Value
    查找超链接所在单元格右侧单元格3列的值-在这种情况下,超链接位于
    列A
    中,文件夹名称位于
    列D

    这是修改后的问题代码,以实现所需的输出

    这是假设您正在工作表中输入值

    Sub DownloadFile()
    
    Dim Pth as String
    Dim Link As Variant
    Dim fname As String
    
    For Each Link In ActiveSheet.Hyperlinks
    fname = Split(Link.Address, "/")(UBound(Split(Link.Address, "/")))
        Pth = "C:\VBA\" & Link.Parent.Offset(0, 3).Value & "\"    'Finds the value of the cell 3 columns to the right of the hyperlink
    
        'The next 3 lines will create the path you specified in your "pth" variable _
        ' or ignore the error if it already exists - this will ensure you don't get errors _
        ' when saving the file to your folder (incase it hasn't been created for example).
        On Error Resume Next
            MkDir (Pth)
        On Error GoTo 0
    
        Filename = Pth & fname
        URLDownloadToFile 0, Link.Address, Filename, 0, 0
    Next Link
    End
    End Sub
    
    为未来读者澄清这些变化


    Pth
    分配为
    Pth=“C:\VBA\”&Link.Parent.Offset(0,3).Value&“\”
    For…Next
    循环中,允许我们根据
    列D
    值动态选择文件夹
    Link.Parent.Offset(0,3).Value
    查找超链接所在单元格右侧单元格3列的值-在这种情况下,超链接位于
    列A
    中,文件夹名称位于
    列D

    您是否可以预先定义文件名的位置?例如,是否将文件“ABC”始终下载到文件夹“123”?Samuel Everson,首先感谢您的即时回复,否,根据订单号,我必须将文件移动到特定文件夹。例如,如果D列i具有唯一的编号/名称,则该ABC文件应移动到名为文件夹的特定D列。此D列文件夹名称将持续更改。我已经复制了这张图片供你在问题中参考。谢谢:ABC和GHI url的所有文件都应该移动到文件夹123。简单地说,基于D列,我必须创建文件夹并将文件移动到该文件夹。因此,对于“ABC”示例,您希望路径为
    C:\Mydocuments\123\Image1
    ?是的,Samuel Eversion您分析的内容非常完美……您是否能够根据文件名的位置预先定义任何内容?例如,是否将文件“ABC”始终下载到文件夹“123”?Samuel Everson,首先感谢您的即时回复,否,根据订单号,我必须将文件移动到特定文件夹。例如,如果D列i具有唯一的编号/名称,则该ABC文件应移动到名为文件夹的特定D列。此D列文件夹名称将持续更改。我已经复制了这张图片供你在问题中参考。谢谢:ABC和GHI url的所有文件都应该移动到文件夹123。简单地说,基于D列,我必须创建文件夹并将文件移动到该文件夹。因此,对于您的“ABC”示例,您希望路径为
    C:\Mydocuments\123\Image1
    ?是的,Samuel Eversion您分析的非常完美……先生,您所做的工作很好,但它正在文件夹内创建文件夹。就像它创建文件夹123一样,对于下一个url,它将在123中创建789文件夹。它应该在同一路径(“C:\Mydocuments\”中,但与789的文件夹名称不同。您能调整一下吗?先生,您真的很棒..效果很好..脱帽致敬先生..我在代码中对Filename=pth&fname改为(=pth&Link.Range.Offset(,1)&“&ext)然后它从源代码中提取了文件。真的非常感谢您的辛勤工作和时间…我将此标记为已接受。再次感谢。先生,您所做的工作很好,但它正在文件夹中创建文件夹。就像它创建文件夹123一样,对于下一个url,它正在123中创建789文件夹。哪个应该在同一个文件夹中路径(“C:\Mydocuments\”,但文件夹名不同,如789。您能调整一下吗?先生,您真的很棒..效果很好..脱帽致敬,先生..我对行Filename=pth&fname的代码做了一些修改(=pth&Link.Range.Offset(,1)和“.”ext)然后从源代码中提取文件很有效。非常感谢您的辛勤工作和时间…我将此标记为已接受。再次感谢。