我正在嘗試創建一個按鈕,單擊該按鈕可讓您瀏覽要復制到指定檔案夾的檔案。我有一個如下所示的作業代碼,但它一次只允許復制一個檔案。我希望能夠一次選擇多個檔案。我似乎無法想出一種方法來整合dialogBox.AllowMultiSelect = True來做到這一點。關于如何做到這一點的任何想法?謝謝你。
Sub UploadFile()
Dim dialogBox As FileDialog
Dim startpath As String
Dim startname As String
Dim destinationfolder As String
Dim FSO
Set dialogBox = Application.FileDialog(msoFileDialogOpen)
Set FSO = CreateObject("Scripting.FileSystemObject")
destinationfolder = "C:\Users\John\Desktop\Images\"
dialogBox.AllowMultiSelect = False 'Do not allow multiple files to be selected
dialogBox.Title = "Select a file to upload" 'Set the title of the DialogBox
dialogBox.InitialFileName = "C:\Users\John\Desktop" 'Set the default folder to open
dialogBox.Filters.Clear 'Clear the dialog box filters
If dialogBox.Show = -1 Then 'Show the dialog box and output full file name
startpath = dialogBox.SelectedItems(1)
End If
startname = Right(startpath, Len(startpath) - InStrRev(startpath, "\")) 'takes filename from startpath
If Not FSO.FileExists(startpath) Then 'Checking If File Is Located in the Source Folder
MsgBox "File Not Found", vbInformation, "Not Found"
ElseIf Not FSO.FileExists(destinationfolder & startname) Then 'Copying If the Same File is Not Located in the Destination Folder
FSO.CopyFile (startpath), destinationfolder, True
MsgBox "File Uploaded Successfully", vbInformation, "Done!"
Else
MsgBox "File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
End Sub
uj5u.com熱心網友回復:
您可以為 SelectedItems 中的每個專案回圈:
Dim dialogBox As FileDialog
Dim startpath As Variant
Dim startname As String
Dim destinationfolder As String
Dim FSO
Set dialogBox = Application.FileDialog(msoFileDialogOpen)
Set FSO = CreateObject("Scripting.FileSystemObject")
destinationfolder = "C:\Users\John\Desktop\Images\"
dialogBox.AllowMultiSelect = True 'Do not allow multiple files to be selected
dialogBox.Title = "Select a file to upload" 'Set the title of the DialogBox
dialogBox.InitialFileName = "C:\Users\John\Desktop\" 'Set the default folder to open
dialogBox.Filters.Clear 'Clear the dialog box filters
If dialogBox.Show = -1 Then 'Show the dialog box and output full file name
For Each startpath In dialogBox.SelectedItems
Debug.Print startpath
startname = Right(startpath, Len(startpath) - InStrRev(startpath, "\")) 'takes filename from startpath
If Not FSO.FileExists(startpath) Then 'Checking If File Is Located in the Source Folder
MsgBox "File Not Found (" & startpath & ")", vbInformation, "Not Found"
ElseIf Not FSO.FileExists(destinationfolder & startname) Then 'Copying If the Same File is Not Located in the Destination Folder
FSO.CopyFile (startpath), destinationfolder, True
MsgBox "File Uploaded Successfully (" & startpath & ")", vbInformation, "Done!"
Else
MsgBox "File Already Exists In The Destination Folder (" & startpath & ")", vbExclamation, "File Already Exists"
End If
Next startpath
End If
我允許多選,并使用 foreach 回圈遍歷每個專案(foreach 要求其回圈變數是一個變體;所以我更改了它的宣告)。
我還將檔案名添加到您的訊息中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467231.html
上一篇:使用IP地址連接資料庫
