After using Application.FileDialog(msoFileDialogFilePicker), how can I save that selected file to a predetermined path?

Viewed 37

I have code in MS Access to select a PDF file. What I cannot figure out is how to then save the selected PDF file to a predetermined path, in this case "mypath". I also need to rename the selected file to the "MyFileName3" string. How can I go about this?

Sub Browse()

Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim SourceFileName As String
Dim mypath As String
Dim MyFileName3 As String

mypath = "\\Improvement Processes\ClaimsHolding\Local\"

MyFileName3 =  & "-03-DealSheet.pdf"

Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
    .AllowMultiSelect = False
    .Title = "Please select your PDF file"
    .Filters.Clear
    .Filters.Add "PDF File", "*.pdf"
    
    If .Show = True Then
        For Each varFile In .SelectedItems
            SourceFileName = varFile
        Next
        
    Else
        MsgBox "You clicked Cancel."
    End If
    End With
    MsgBox SourceFileName


End Sub
1 Answers

You can do it easily with FileSystemObject

Here is an example of how to implement it in your code:

   Dim FSO As Object, SourceFile As Object
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set SourceFile = FSO.GetFile(SourceFileName)
   SourceFile.Copy Destination:=mypath
   FSO.GetFile(mypath & SourceFile.Name).Name = MyFileName3

Make sure that MyFileName3 includes the file extension .pdf or you will create an untyped file when you rename it.

Related