Solved How to copy a file when closing a File Dialog? (1 Viewer)

YuvalH

New member
Local time
Today, 08:11
Joined
Jan 21, 2022
Messages
13
I want to copy a selected file to a folder and I think a good time for it is when I'm closing the File Dialog. How do I do this?

File Dialog entry and exit:
Code:
Private Sub File_Dialog_Click()
    Dim fd As FileDialog
    Dim varFile As Variant
 
 
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
 
    With fDialog
        .AllowMultiSelect = False
        .Title = "Please select one or more files"
      
        .Filters.Clear
        .Filters.Add "All Files", "*.*"
      
        If .Show = True Then
            For Each varFile In .SelectedItems
                Me.File.Value = .SelectedItems.Item(1)
            Next
        Else
            MsgBox "You clicked Cancel in the file dialog box."
        End If
    End With
End Sub

Private Sub File_Dialog_Exit(Cancel As Integer)
    Me.File_Name = Mid([File], InStrRev([File], "\") + 1, InStrRev([File], ".") - InStrRev([File], "\") - 1)
    Me.File_Type = Right([File], Len([File]) - InStrRev([File], "."))
End Sub

Destiny folder:
Code:
Public Sub Create_Folder(sFolder As String)
    If Len(Dir(sFolder, vbDirectory)) = 0 Then
        MkDir sFolder
    End If
End Sub

Public Sub FolderCheck()
    Create_Folder ("E:\" & Format(Date, "yyyy"))
    Create_Folder ("E:\" & Format(Date, "yyyy") & "\" & Format(Date, "mm") & " " & Format(Date, "mmmm"))
End Sub

thx for help
 
Last edited:

June7

AWF VIP
Local time
Yesterday, 21:11
Joined
Mar 9, 2014
Messages
4,377
Can use FileCopy() or CopyFile() methods.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:11
Joined
Feb 28, 2001
Messages
22,799
In your file dialog click routine, after the End With but before you do the Exit Sub, you could do a file copy in either of the ways as suggested by June7. However, I'm not sure what you were doing with that loop dealing with Me.File.Value, so I'm going to back away a little before giving any more advice.

It also looks like you have code trying to extract file name and file type, which is a reasonable thing to ask. However, if you look up the FileSystemObject (FSO), which you get from the Windows Scripting library, you would find that there are routines that extract the file name and type for you without such complex expressions. Having FSO present also lets you manipulate, copy, delete, and examine the properties of files. May I therefore respectfully suggest you take a look at the FileSystemObject?


If you start from the linked page and just explore a little, you will find all of the wonderful file-related things you can do with FSO.
 

Eugene-LS

Registered User.
Local time
Today, 08:11
Joined
Dec 7, 2018
Messages
351
I want to copy a selected file to a folder and I think a good time for it is when I'm closing the File Dialog. How do I do this?
...
Try that code please:
Code:
Private Sub File_Dialog_Click()
Const scDistDiskPart$ = "E:"
Dim sFilePathAndName$, sFileName$, sFileExt$
Dim sFileDistPath$, sVal$
Dim objFSO As Object  ' File System Object
'----------------------------------------------------------------------------------------------
On Error GoTo File_Dialog_Click_Err

    With Application.FileDialog(1) '1 = msoFileDialogOpen
        .Title = "Please select one or more files" 'Window title
        .InitialFileName = CurrentProject.Path     'Starting path
        .AllowMultiSelect = False                  'There cannot be multiple files - Only ONE!
        .Filters.Clear
        .Filters.Add "All Files", "*.*", 1
        'Ïîåõàëè!
        .Show
        If .SelectedItems.Count > 0 Then
            sFilePathAndName = .SelectedItems(1)
        Else
            MsgBox "You clicked Cancel in the File Open dialog box."
            Exit Sub
        End If
    End With

    sFileName = Mid(sFilePathAndName, InStrRev(sFilePathAndName, "\") + 1,  _
            InStrRev(sFilePathAndName, ".") - InStrRev(sFilePathAndName, "\") - 1)
    sFileExt = Right(sFilePathAndName, Len(sFilePathAndName) - InStrRev(sFilePathAndName, "."))
    'Debug.Print sFileName & "." & sFileExt

    Set objFSO = CreateObject("Scripting.FileSystemObject")

'Creating folders:
    sFileDistPath = scDistDiskPart & "\" & Format(Date, "yyyy")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath
  
    sFileDistPath = sFileDistPath & "\" & Format(Date, "mm")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath

    sFileDistPath = sFileDistPath & "\" & Format(Date, "mmmm")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath
  
    DoEvents
    'Debug.Print sFileDistPath
  
'Copying:
    sVal = sFileDistPath & "\" & sFileName & "." & sFileExt
    objFSO.CopyFile sFilePathAndName, sVal
    DoEvents
  
    If Not Dir(sVal) = "" Then
        MsgBox "Copying completed successfully!", vbInformation
    Else
       GoTo File_Dialog_Click_End
    End If
  
'Filling up form fields:
'    Me.File_Name = sFileName
'    Me.File_Type = sFileExt

File_Dialog_Click_End:
    On Error Resume Next
    Set objFSO = Nothing
    Err.Clear
    Exit Sub

File_Dialog_Click_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub" & _
           "File_Dialog_Click - 00_Tests.", vbCritical, "Ïðîèçîøëà îøèáêà!"
    Err.Clear
    Resume File_Dialog_Click_End
End Sub
 

YuvalH

New member
Local time
Today, 08:11
Joined
Jan 21, 2022
Messages
13
In your file dialog click routine, after the End With but before you do the Exit Sub, you could do a file copy in either of the ways as suggested by June7. However, I'm not sure what you were doing with that loop dealing with Me.File.Value, so I'm going to back away a little before giving any more advice.

It also looks like you have code trying to extract file name and file type, which is a reasonable thing to ask. However, if you look up the FileSystemObject (FSO), which you get from the Windows Scripting library, you would find that there are routines that extract the file name and type for you without such complex expressions. Having FSO present also lets you manipulate, copy, delete, and examine the properties of files. May I therefore respectfully suggest you take a look at the FileSystemObject?


If you start from the linked page and just explore a little, you will find all of the wonderful file-related things you can do with FSO.
The 'Me.File.Value' takes the path and enters him into a field called "File" in a table.
 

YuvalH

New member
Local time
Today, 08:11
Joined
Jan 21, 2022
Messages
13
Try that code please:
Code:
Private Sub File_Dialog_Click()
Const scDistDiskPart$ = "E:"
Dim sFilePathAndName$, sFileName$, sFileExt$
Dim sFileDistPath$, sVal$
Dim objFSO As Object  ' File System Object
'----------------------------------------------------------------------------------------------
On Error GoTo File_Dialog_Click_Err

    With Application.FileDialog(1) '1 = msoFileDialogOpen
        .Title = "Please select one or more files" 'Window title
        .InitialFileName = CurrentProject.Path     'Starting path
        .AllowMultiSelect = False                  'There cannot be multiple files - Only ONE!
        .Filters.Clear
        .Filters.Add "All Files", "*.*", 1
        'Ïîåõàëè!
        .Show
        If .SelectedItems.Count > 0 Then
            sFilePathAndName = .SelectedItems(1)
        Else
            MsgBox "You clicked Cancel in the File Open dialog box."
            Exit Sub
        End If
    End With

    sFileName = Mid(sFilePathAndName, InStrRev(sFilePathAndName, "\") + 1,  _
            InStrRev(sFilePathAndName, ".") - InStrRev(sFilePathAndName, "\") - 1)
    sFileExt = Right(sFilePathAndName, Len(sFilePathAndName) - InStrRev(sFilePathAndName, "."))
    'Debug.Print sFileName & "." & sFileExt

    Set objFSO = CreateObject("Scripting.FileSystemObject")

'Creating folders:
    sFileDistPath = scDistDiskPart & "\" & Format(Date, "yyyy")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath
 
    sFileDistPath = sFileDistPath & "\" & Format(Date, "mm")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath

    sFileDistPath = sFileDistPath & "\" & Format(Date, "mmmm")
    If Dir(sFileDistPath, vbDirectory) = "" Then objFSO.CreateFolder sFileDistPath
 
    DoEvents
    'Debug.Print sFileDistPath
 
'Copying:
    sVal = sFileDistPath & "\" & sFileName & "." & sFileExt
    objFSO.CopyFile sFilePathAndName, sVal
    DoEvents
 
    If Not Dir(sVal) = "" Then
        MsgBox "Copying completed successfully!", vbInformation
    Else
       GoTo File_Dialog_Click_End
    End If
 
'Filling up form fields:
'    Me.File_Name = sFileName
'    Me.File_Type = sFileExt

File_Dialog_Click_End:
    On Error Resume Next
    Set objFSO = Nothing
    Err.Clear
    Exit Sub

File_Dialog_Click_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub" & _
           "File_Dialog_Click - 00_Tests.", vbCritical, "Ïðîèçîøëà îøèáêà!"
    Err.Clear
    Resume File_Dialog_Click_End
End Sub
Thank you very much, it worked!
 

Users who are viewing this thread

Top Bottom