SimoneRene
Registered User.
- Local time
- Today, 10:32
- Joined
- Mar 15, 2017
- Messages
- 58
Does anyone know of a way I can change the destination (targetfile) in my code so the user can choose the target file location each time. Currently its set to one folder location.
I thought about a msoFolderPicker but I dont know how to integrate it into my existing code.
Thanks for any help!
I thought about a msoFolderPicker but I dont know how to integrate it into my existing code.
Code:
Private Sub SaveNewIssBtn_Click()
Dim CopyDialog As office.FileDialog
Dim SourceFile As Variant
Dim targetFile As Variant
Dim varFile As Variant
Dim objDialog As Object
Dim Response As Integer
FileViewerTxt = "" 'Text box blank
targetFile = "X:\Folder\Doc Folder\" 'Add File Destiation
ChDir Dir("*.*", vbDirectory)
Set CopyDialog = Application.FileDialog(msoFileDialogFilePicker) 'Open file picker and user chooses file to copy
Set objDialog = Application.FileDialog(msoFileDialogFilePicker) 'Open file picker and user chooses file to display in text box (the copy and display occur in the same action so in reality the user only see's one file picker)
With CopyDialog
With objDialog
.AllowMultiSelect = False 'User can only select one file, for multiple files change to true
.Title = "Select File" 'selects the file name from the whole file path
.Filters.Add "pdf File", "*.pdf" 'Adds file type(pdf) to the end of the file name so in table "documentname.pdf" is shown
If .Show = False Then 'no file selected and file picker closed
MsgBox "File Upload Canceled" 'User didn't select a file so message box shows
FileViewerTxt = Null 'Sets Textbox that displays file to null so that record can't be saved as file upload required(see saveBtn_Click()
End If
For Each SourceFile In .SelectedItems 'Search targetfile(file destination) for selected file to make sure user isn't duplicating a file
If Len(Dir(targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\")))) <> 0 Then 'If there is a match then...
MsgBox "A Document with the same File Name has already been Issued, Please Rename and Try Again!" 'Display message box alerting user of duplicate file and cancel upload
Exit Sub
End If
Response = MsgBox(Prompt:="Are you sure you want to Save " & SourceFile & " in Issued Documents?", Buttons:=vbYesNo) 'Prompt user to confirm save
If Response = vbNo Then 'user answers no
MsgBox ("Upload Canceled!") 'message 'upload canceled'
FileViewerTxt = Null 'Sets Textbox that displays file to null so that record can't be saved as file upload required(see saveBtn_Click()
End If
If Response = vbYes Then 'User confirmed action to upload(Filecopy)
MsgBox ("Upload Successful!") 'Let user know upload was successful
Me.FileViewerTxt = Dir(.SelectedItems(1)) 'Display file selected in textbox 'FileViewerTxt'
For Each varFile In .SelectedItems
FileCopy SourceFile, targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\")) 'Copy selcted file to target file(file destination defined above)
Set objDialog = Nothing 'Reset object dialog
Next
End If
Next
End With
End With
End Sub