excelvbaexcel-formulavba7vba6

copy file from a folder to another knowing the file name typed in a text box


enter image description hereenter image description hereenter image description hereenter image description hereI am trying to create a code to copy a pdf file from a folder to another folder keeping same name and everything. when I type the file name in a textbox and click on a button I want the code to copy this file from a folder to another.

my code is copying ALL the files in the source folder to the destination folder and renaming it by the textbox name that I have typed (txtledger.value) & the file original name. I dont know how to fix it.

Sub CreatingFSO()
Dim MyFSO As FileSystemObject
Set MyFSO = New FileSystemObject
End Sub

Sub CopyFiles()
Dim MyFSO As FileSystemObject
Dim MyFile As File
Dim SourceFolder As String
Dim DestinationFolder As String
Dim MyFolder As Folder

Set MyFile = txtLedger.Value

SourceFolder = "F:\4-2022"
DestinationFolder = "F:\DELEGATION APPLICATION\" & MyFile



Set MyFSO = New Scripting.FileSystemObject
Set MyFolder = MyFSO.GetFolder(SourceFolder)

For Each MyFile In MyFolder.Files
    MyFSO.CopyFile Source:=MyFSO.GetFile(MyFile), _
    Destination:=DestinationFolder & MyFile.Name, Overwritefiles:=False
Next MyFile

End Sub

I'm new to this so I don't know how what to do. any help please?


Solution

  • You can try something like this. It'll take your text box value, check if that file exists and then copy it over if it does. Depending upon what you're trying to do, you could have it overwrite it if the source PDFs may change or something like that.

        Sub CopyFile()
    
        Dim MyFSO As FileSystemObject
        Dim SourceFolder As String
        Dim DestinationFolder As String
        Dim SourceFile As File
        Dim DestinationFile As String
        Dim FileName As String
        
        
    
        SourceFolder = "F:\4-2022\"
        DestinationFolder = "F:\DELEGATION APPLICATION\2-2023\"
        
        FileName = frmDELEGATION.txtLedger.Value
        
                
        If Not FileExists(SourceFolder, FileName) Then
        
            MsgBox "No Documents Found ."
            Exit Sub
        
        End If
    
    
        Set MyFSO = New FileSystemObject
        
        Set SourceFile = MyFSO.GetFile(SourceFolder & FileName & ".pdf")
        
          
        
                 
        DestinationFile = DestinationFolder & SourceFile.Name
    
    
        MyFSO.CopyFile Source:=SourceFile.Path, Destination:=DestinationFile, OverwriteFiles:=False
        MsgBox "Documents Saved !"
    
    End Sub
    
    Function FileExists(ByVal folderpath As String, ByVal FileName As String) As Boolean
    
        FileExists = (Dir(folderpath & FileName & ".pdf") <> "")
        
    
    End Function
    

    Explanation:

    **Updated to add form values to variable

    **After the changes are made, make sure the value is just the name and extension. So FILENAME.pdf is what should be in the text box. No spaces.

    EDIT

    Try this for FileExists:

    Function FileExists(ByVal FolderPath As String, ByVal FileName As String) As Boolean
        FileExists = (Dir(FolderPath & FileName) <> "")
    End Function
    

    Also, update its use above:

    ' Check if the source file exists
        If Not FileExists(SourceFolder, FileName) Then
            MsgBox "The specified file does not exist."
            Exit Sub
        End If