+ Reply to Thread
Results 1 to 8 of 8

Copying files from a folder without destination

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2016
    Location
    canada
    MS-Off Ver
    2013
    Posts
    63

    Copying files from a folder without destination

    I searched quite a bit, but can't find the answer to my problem. I want to copy all the files from a folder to memory or clipboard and paste it manually wherever I like to paste it.

    the codes that I have found, all have a destination folder. How can i achieve this?

    thanks

    Sub CopyFile()
    
        Dim fso As Object
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        fso.CopyFile "S:\PDF_Jobs\" & ComboBox1 & "\" & ComboBox2 & "\" & ComboBox3 & "\", DESTINATION FOLDER
    
        Set fso = Nothing
    
    End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,944

    Re: Copying files from a folder without destination

    What about this? Add an Input box to ask where you want to paste the files.

    Sub CopyFile()
    
        Dim fso As Object
        Dim sPath As String
        sPath = InputBox("Type your destination folder here using the full path")
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        fso.CopyFile "S:\PDF_Jobs\" & ComboBox1 & "\" & ComboBox2 & "\" & ComboBox3 & "\", sPath
        Set fso = Nothing
    
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    06-15-2016
    Location
    canada
    MS-Off Ver
    2013
    Posts
    63

    Re: Copying files from a folder without destination

    That wouldn't save my time as I have to browse folders on the other program(folders created randomly) and then paste it on the input box. I want to CTRL+V the files.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,944

    Re: Copying files from a folder without destination

    Sorry, but I don't have a better solution for you.

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copying files from a folder without destination

    Maybe :
    Private Type POINTAPI
      x As Long
      y As Long
    End Type
    Private Type DROPFILES
      pFiles As Long
      pt As POINTAPI
      fNC As Long
      fWide As Long
    End Type
    Private Const CF_HDROP = 15
    Private Const GMEM_MOVEABLE = &H2
    Private Const GMEM_ZEROINIT = &H40
    Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
    Private Declare Function CloseClipboard Lib "user32" () As Long
    Private Declare Function EmptyClipboard Lib "user32" () As Long
    Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
    Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
    Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
    Private Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Function GetFilesFromClipboard(strFiles() As String) As Long
      Const MAX_PATH As Long = 260
      Dim pt As POINTAPI, hDrop As Long, nFiles As Long, nNul As Long, i As Long, desc As String, strFilename As String
      If IsClipboardFormatAvailable(CF_HDROP) Then
         If OpenClipboard(0&) Then
            hDrop = GetClipboardData(CF_HDROP)
            nFiles = DragQueryFile(hDrop, -1&, "", 0)
            ReDim strFiles(1 To nFiles) As String
            strFilename = Space$(MAX_PATH)
            For i = 1 To nFiles
                Call DragQueryFile(hDrop, i - 1, strFilename, Len(strFilename))
                nNul = InStr(strFilename, vbNullChar)
                Select Case nNul
                  Case Is > 1: strFiles(i) = Left$(strFilename, nNul - 1)
                  Case 1: strFiles(i) = ""
                  Case 0: strFiles(i) = Trim$(strFiles(i) = "")
                End Select
            Next
            Call CloseClipboard
         End If
         GetFilesFromClipboard = nFiles
      End If
    End Function
    Public Function SetFilesToClipboard(strFiles() As String) As Boolean
      Dim df As DROPFILES, hGlobal As Long, lpGlobal As Long, i As Long, strData As String
      If OpenClipboard(0&) Then
         EmptyClipboard
         For i = LBound(strFiles) To UBound(strFiles)
             strData = strData & strFiles(i) & vbNullChar
         Next
         strData = strData & vbNullChar
         hGlobal = GlobalAlloc(GHND, Len(df) + Len(strData))
         If hGlobal Then
            lpGlobal = GlobalLock(hGlobal)
            df.pFiles = Len(df)
            CopyMem ByVal lpGlobal, df, Len(df)
            CopyMem ByVal (lpGlobal + Len(df)), ByVal strData, Len(strData)
            GlobalUnlock hGlobal
            If SetClipboardData(CF_HDROP, hGlobal) Then SetFilesToClipboard = True
         End If
         CloseClipboard
      End If
    End Function
    Sample usage :
    Sub Test_GetFilesFromClipboard()
      Dim strFile() As String, i As Long, v
      i = GetFilesFromClipboard(strFile)
      If i > 0 Then
         For Each v In strFile
             Debug.Print v
         Next v
      End If
    End Sub
    
    Sub Test_SetFilesToClipboard()
      Dim strFile(1 To 2) As String
      strFile(1) = "Z:\1.txt"
      strFile(2) = "Z:\2.txt"
      If SetFilesToClipboard(strFile) Then Debug.Print "Success" Else Debug.Print "Failed"
    End Sub
    
    Sub Test_EF1166264()
      Dim strPattern As String, strPath As String, strFile() As String, i As Long, vFile
      strPattern = "Z:\*.*"  'Change this to something like strPattern = "S:\PDF_Jobs\" & ComboBox1 & "\" & ComboBox2 & "\" & ComboBox3 & "\*.*"
      strPath = Left$(strPattern, InStrRev(strPattern, "\"))
      ReDim strFile(1 To 1000) As String
      vFile = Dir(strPattern)
      While vFile <> ""
        i = i + 1
        strFile(i) = strPath & vFile
        vFile = Dir
      Wend
      If i > 0 Then ReDim Preserve strFile(1 To i) As String
      For i = 1 To UBound(strFile): Debug.Print strFile(i): Next i
      If i > 0 Then SetFilesToClipboard strFile
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  6. #6
    Registered User
    Join Date
    06-15-2016
    Location
    canada
    MS-Off Ver
    2013
    Posts
    63

    Re: Copying files from a folder without destination

    can you explain how i could make this work on vba? where are there two codes?

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copying files from a folder without destination

    Did you ever work with Excel ?



  8. #8
    Registered User
    Join Date
    06-15-2016
    Location
    canada
    MS-Off Ver
    2013
    Posts
    63

    Re: Copying files from a folder without destination

    nothing major. I created a userform with the info that i found on here and google.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copying Files : Userdefined Location to user defined destination folder
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2016, 05:31 PM
  2. [SOLVED] Creating new folder, copying files and renaming files in folder
    By LionelNZ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2015, 04:18 PM
  3. [SOLVED] Copy certain files from Source folder to Destination folder
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-16-2015, 08:56 PM
  4. Tracking what files are in a apecific destination folder
    By winwall in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-26-2014, 05:28 PM
  5. Replies: 1
    Last Post: 11-12-2013, 03:44 PM
  6. Replies: 0
    Last Post: 09-16-2013, 02:24 AM
  7. copying data from excel files in a source folder to target folder
    By Javed07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2013, 04:27 PM

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