2017-05-24 108 views
-1

我对VBA编码非常陌生,并且试图帮助尝试拥有一个将从网络驱动器下载/复制多个图像的宏的朋友,将它们重命名为并将它们保存到桌面上的文件夹中。Excel宏将图像从网络驱动器复制到其他文件夹

电子表格中的数据按此设置。 宏将图像从B列中列出的路径复制,然后重命名与列的数据图像,并将其保存到一个文件夹在桌面上

column A -   column B  -    column C 

3487458 - N:/path1/image1.jpg - http://www.website.com/data.pdf 

5412132 - N:/path2/image2.jpg - http://www.website.com/data2.pdf 

我有这个,在列的数据工作C是html链接,但我需要它来处理列B中作为网络驱动器路径的数据。

Const TargetFolder = "C:\Users\XXXX\Desktop\Output\" 
Private 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 


Sub Test() 
    For Each Hyperlink In ActiveSheet.Hyperlinks 
     LocalFileName = ActiveSheet.Cells(Hyperlink.Range.Row, 1).Value & 
".pdf" 
    Call HTTPDownloadFile(Hyperlink.Address, TargetFolder & LocalFileName) 
    Next Hyperlink 
End Sub 

谢谢,罗伊

编辑:CODE奏效

Sub copythem() 
Dim rw As Long, start_row As Long, end_row As Long 
Dim destination_folder As String 
Dim suffix As String 

suffix = ".jpg" 

With ActiveSheet 
    start_row = 1 
    end_row = .Cells(.Rows.Count, "B").End(xlUp).Row 

    destination_folder = "C:\Users\XXXX\Desktop\Output\" ' Set destination as user's desktop 

    For rw = start_row To end_row 
     If Dir(.Cells(rw, 2)) <> "(.Cells(rw, 2))" Then 
      FileCopy .Cells(rw, 2), destination_folder & .Cells(rw, 1) & suffix 
     Else 
      MsgBox "File: " & .Cells(rw, 2) & " is not found." 
     End If 
    Next 

End With 
End Sub 
+0

看看[这个网站](https://www.r ondebruin.nl/win/s3/win026.htm)。这只是一两行代码来移动/复制VBA中的文件。 – JNevill

+0

当您使用电子表格(VBS是另一种语言)时,我已将您的VBS引用更改为VBA。 –

+0

谢谢澄清。 – Buddhak

回答

0

不是很多去,而是让在路上几个假设,如文件类型(suffix),当你说的'桌面' - 你的意思是在最近版本的Windows上的桌面..

Sub copythem() 
    Dim rw As Long, start_row As Long, end_row As Long 
    Dim destination_folder As String 
    Dim suffix As String 

    suffix = ".jpg" 

    With ActiveSheet 
     start_row = 1 
     end_row = .Cells(.Rows.Count, "B").End(xlUp).Row 

     destination_folder = Environ("homedrive") & Environ("homepath") & "\desktop\output\" ' Set destination as output folder in user's desktop 

     For rw = start_row To end_row 
      If Dir(.Cells(rw, 2)) <> "" Then 
       FileCopy .Cells(rw, 2), destination_folder & .Cells(rw, 1) & suffix 
      Else 
       MsgBox "File: " & .Cells(rw, 2) & " is not found." 
      End If 
     Next 

    End With 
End Sub 
+0

如果您决定将destination_folder指向其他内容,请确保该文件夹存在,然后再执行此操作。 (rw,2),destination_folder&.Cells(rw,1)&后缀 – CLR

+0

后缀更改为.jpg destination_folder =“C:\ Users \ rykenes \桌面\输出\“'设置目的地作为用户的桌面 是我编辑的唯一 – Buddhak

+0

经过一些调整和阅读后,我得到它的工作正是根据需要。非常感谢你。 – Buddhak

相关问题