+ Reply to Thread
Results 1 to 5 of 5

Copy selected files into a specific directory

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Copy selected files into a specific directory

    Hi,
    I need a procedure to:
    a) let the user select any file (name or type)
    b) Paste the selected file in a Specific folder.

    Part a) is easy. I used the
     Application.FileDialog(msoFileDialogFilePicker)
    line.
    How can i accomplish part b)?? I dont need to open the files. Just to save them in the folder I choose. Those files could be any type. From .pdf to .fgs to anythig.

    Thanks
    Last edited by Wizz; 01-29-2009 at 10:50 AM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Copy selected files into a specific directory

    Heres 1 method - It moves the file

    Place all the code in a normal module
    Run MoveFile


    Private Type BrowseInfo ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
        Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
    
    
    Sub TestGetFolderName()
    Dim FolderName As String
        FolderName = GetFolderName("Select a folder")
        If FolderName = "" Then
            MsgBox "You didn't select a folder."
        Else
            MsgBox "You selected this folder: " & FolderName
        End If
    End Sub
    
    Function GetFolderName(Msg As String) As String
    ' returns the name of the folder selected by the user
    Dim bInfo As BrowseInfo, Path As String, r As Long
    Dim x As Long, pos As Integer
        bInfo.pidlRoot = 0& ' Root folder = Desktop
        If IsMissing(Msg) Then
            bInfo.lpszTitle = "Select a folder."
            ' the dialog title
        Else
            bInfo.lpszTitle = Msg ' the dialog title
        End If
        bInfo.ulFlags = &H1 ' Type of directory to return
        x = SHBrowseForFolder(bInfo) ' display the dialog
        ' Parse the result
        Path = Space$(512)
        r = SHGetPathFromIDList(ByVal x, ByVal Path)
        If r Then
            pos = InStr(Path, Chr(0))
            GetFolderName = Left(Path, pos - 1)
        Else
            GetFolderName = ""
        End If
    End Function
    
    
    Sub MoveFile()
    Dim sFile As String
    Dim sFname As String
    Dim sToFolder As String
    Dim iPos As Integer
    
    sFile = Application.GetOpenFilename
    If sFile = "False" Then
       Exit Sub
    End If
    iPos = InStrRev(sFile, "\")
    sFname = Right(sFile, Len(sFile) - (iPos))
    sToFolder = GetFolderName("Select Folder")
    If sToFolder = "" Then
       Exit Sub
    End If
    
    Name sFile As sPath & sToFolder & "\" & sFname
    
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Copy selected files into a specific directory

    Hi Mudraker,
    I Must NOT modify the original. Is there a form to have a duplicated file in the new folder?

  4. #4
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: Copy selected files into a specific directory

    Hi
    If you don't have too many files, Let
    A1= source file path (G:\macro\upload\)
    A2= source file name(with extention)(wizz.xls)
    A3= Destination file path(F:\coffee\sugar\)
    A4 = destination file name(with extention)(Wizard.xls)

    Sub macro3()
    FileCopy Cells(1, 1) & Cells(2, 1), Cells(3, 1) & Cells(4, 1)
    MsgBox "File copy completed"
    End Sub
    Ravi

  5. #5
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Copy selected files into a specific directory

    Excelente !
    Just what I needed.

    Thanks you very much Ravishankar and Mudraker.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1