We help IT Professionals succeed at work.
Get Started

Copy Multiple Files To New Destination

G Scott
G Scott asked
Last Modified: 2013-11-27
So I was just playing around to see IF I can copy multiple files to a new location and I have got it working. The only problem with my code is you have to select the destination first. When I put the folder selection after the file selection it prompts me for a new destination for each file I have selected. How can I do this where I can select the files and then choose the destination? A HUGE thanks goes out to Helen Feddema for posting code that I could piece together.

Public Function SelectFile() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009
'On Error GoTo ErrorHandler

   Dim fd As Office.FileDialog
   Dim varSelectedItem As Variant
   Dim strFileNameAndPath As String
    Dim strFolderPath As String
   Dim fds As Office.FileDialog
   Dim strPath As String
   Dim SelectFolder As String
   'Create a FileDialog object as a File Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Set fds = Application.FileDialog(msoFileDialogFolderPicker)
      strPath = "c:\"

   With fds
      .Title = "Browse for folder where _________ are located"
      .ButtonName = "Select"
      .InitialView = msoFileDialogViewDetails
      '.InitialFileName = strPath
      If .Show = -1 Then
         strFolderPath = CStr(fd.SelectedItems.Item(1)) & "\"
         Debug.Print "User pressed Cancel"
         strFolderPath = ""
      End If
   End With
   SelectFolder = strFolderPath
   MsgBox SelectFolder
      Set fd = Application.FileDialog(msoFileDialogFilePicker)
   With fd
      'Set AllowMultiSelect to True to allow selection of multiple files
      .AllowMultiSelect = True
      .Title = "Browse for File"
      .ButtonName = "Select"
      .Filters.Add "Documents", "*.*", 1
'       .Filters.Add "Documents", "*.doc; *.txt", 1
      .InitialView = msoFileDialogViewDetails
      If .Show = -1 Then
         'Get selected item in the FileDialogSelectedItems collection
         For Each varSelectedItem In .SelectedItems
            strFileNameAndPath = CStr(varSelectedItem)
            Dim FileNameWithExt
            FileNameWithExt = Mid$(strFileNameAndPath, InStrRev(strFileNameAndPath, "\") + 1)
'            MsgBox FileNameWithExt
'            MsgBox varSelectedItem
'            MsgBox strFileNameAndPath
'            MsgBox SelectFolder
           FileCopy varSelectedItem, SelectFolder & FileNameWithExt
         Next varSelectedItem
         Debug.Print "User pressed Cancel"
         strFileNameAndPath = ""
      End If
   End With
   SelectFile = strFileNameAndPath
'   Set fd = Nothing
'   Exit Function
'   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
'   Resume ErrorHandlerExit

End Function

Open in new window

Thanks for any help on this.
Watch Question
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE