I manage to create a nice file copier, but I have 2 problems, Please Help
I'm using a userform.
I have a button to select the main folder where all the pdf files must go to, and then the location is saved in the registry, so everytime I run the userform the main folder is selected. I am using this code, and it works 100%
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
Dim vrtSelectedItem As Variant
With fd
.Title = "Please select folder"
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
TextBox1.Value = .SelectedItems(1) & IIf(Right(CurDir, 1) <> "\", "\", "")
Next vrtSelectedItem
Else
End If
End With
SaveSetting APPNAME, "Settings", "TextBox1", TextBox1.Value
then I select the folder where the pdf files is with a second button, and only PDF files is selected. I am using this code, and it works 100%
ListBox1.Clear
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Please select folder"
If .Show = -1 Then
ListFiles = Dir("*.pdf")
Do While Not ListFiles = ""
ListBox1.AddItem ListFiles
ListFiles = Dir()
Loop
Else
End If
End With
the Listbox is set to MultiSelect = fmMultiSelectMulti and ListStyle = fmListStyleOption
Then I copy the selected item to the main folder
I am using this code, and it works 100%
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
FileCopy ListBox1.List(i), TextBox1.Value & ListBox1.List(i)
Else
MsgBox "File already in List"
End If
Next i
MsgBox "All selected PDF Files copied to " & """" & TextBox1.Value & """"
ListBox1.Clear
Q1. I want to use the file browser not the filedialog picker. I manage to select the folder, but it lists all the file. How do I get it to select only pdf files, and I also want to list the files in a Listbox on Userform
the code for the browser.
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0&
bInfo.lpszTitle = "Select a folder."
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Sub BrowseFolder()
Dim Directory As String
Dim f As String
Dim r As Long
Directory = GetDirectory
If Directory = "" Then Exit Sub
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
'MsgBox Directory
r = 0
f = Dir(Directory, Range("E1").Value)
Do While f <> ""
r = r + 1
Cells(r, 1) = f
f = Dir
Loop
End Sub
Q2. How do I set the button that copies the files to check if the files is already in the main folder.
Bookmarks