Link to home
Start Free TrialLog in
Avatar of G Scott
G ScottFlag for United States of America

asked on

Copy Multiple Files To New Destination

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)) & "\"
      Else
         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.Clear
      .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
        
         
      Else
         Debug.Print "User pressed Cancel"
         strFileNameAndPath = ""
      End If
   End With
   
   SelectFile = strFileNameAndPath
  
   
'ErrorHandlerExit:
'   Set fd = Nothing
'   Exit Function
'
'ErrorHandler:
'   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
'   Resume ErrorHandlerExit

End Function

Open in new window


Thanks for any help on this.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I like to use the folder selection code running from a command button on the main menu, and saving the folder path to a custom database property.  That way you can get the path wherever you need it in code throughout the database.  Here is a variation of the SelectFolder code that uses this method:
Private Sub cmdDocumentsPath_Click()
'Created by Helen Feddema 12-Jun-2011
'Last modified by Helen Feddema 25-Jun-2011

On Error GoTo ErrorHandler

   'Create a FileDialog object as a Folder Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFolderPicker)
   Set txt = Me![txtDocumentsPath]
   strPropertyName = "DocumentsPath"
   strPath = GetProperty(strPropertyName, "")
   
   With fd
      .Title = "Browse for folder where workbooks and PDF documents " _
         & "should be stored"
      .ButtonName = "Select"
      .InitialView = msoFileDialogViewDetails
      .InitialFileName = strPath
      If .Show = -1 Then
         strPropertyValue = CStr(fd.SelectedItems.Item(1))
         lngDataType = dbText
         Call SetProperty(strPropertyName, lngDataType, _
            strPropertyValue)
         txt.Value = strPropertyValue
      Else
         Debug.Print "User pressed Cancel"
      End If
   End With

ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in " & Me.ActiveControl.Name & " procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

To retrieve the path, use this syntax:

GetProperty("DocumentsPath", "")
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note that you were originally using the SelectFile procedure, which selects a file.  I have another procedure for selecting a folder.
Avatar of G Scott

ASKER

Sorry for the reply delay. Thanks for the solutions. Helen-that is an awesome solution.