1

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?

1 Answers1

1

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:

  • The CopyFile subroutine is called when the button is clicked.
  • It checks if the specified file exists in the source folder using the FileExists function.
  • If the file doesn't exist, it displays an error message and exits the subroutine.
  • If the file exists, it creates an instance of the FileSystemObject. It retrieves the File object for the specified source file.
  • The destination file path is constructed by appending the source file name to the destination folder path.
  • Finally, the CopyFile method is used to copy the file to the destination folder, and a success message is displayed.

**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
Fata1Err0r
  • 836
  • 1
  • 6
  • 14
  • **Comments have been [moved to chat](https://chat.stackoverflow.com/rooms/253730/discussion-on-answer-by-fata1err0r-copy-file-from-a-folder-to-another-knowing-th); please do not continue the discussion here.** Before posting a comment below this one, please review the [purposes of comments](/help/privileges/comment). Comments that do not request clarification or suggest improvements usually belong as an [answer](/help/how-to-answer), on [meta], or in [chat]. Comments continuing discussion may be removed. – Samuel Liew May 19 '23 at 01:45