我有一个允许用户添加文件的表单和一个列出这些添加的文件的列表框:
Private Sub cmdFileDialog_Click()
' Add Files button
' Using this to open the File Dialog box and save attachment file location paths
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim varFileName As String
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow the user to make multiple selections in the dialog box.
.AllowMultiSelect = True
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' This loops through each file that is selected and then add it to the list box.
'For Each varFile In .SelectedItems
' Me.FileList.AddItem varFile
'Next
' This loops through each file that is selected and adds the entire file's path to the invisible list.
' Will use this invisible list to save locations of any attachments to this record in the ideaAttachmentPath field in tblIdeaDetails
For Each varFile In .SelectedItems
Me.InvisiblePathList.AddItem varFile
Next
' This goes through each selected file and extracts just file's name rather than full path name (accomplished above)
' and adds file names to list box
For Each varFile In .SelectedItems
varFileName = Dir(varFile)
Me.FileList.AddItem varFileName
attachmentsAdded = True
Next
Me.ClearListBoxButton.Visible = True
Me.AttachedLabel.Visible = True
Me.FileList.Visible = True
End If
End With
End Sub
我希望完成的是在用户单击按钮(特别是保存按钮)后将这些添加的文件保存到网络文件夹中。如何遍历文件列表框并将这些文件复制到网络文件夹中?这是我到目前为止所拥有的:
Function SaveAttachments()
Dim fileName As Variant
Dim fileDestination As String
Dim attachment As Integer
For attachment = 0 To Me.FileList.ListCount
'MsgBox (FileList.ItemData(x))
FileList.ItemData(attachment).Text = fileName
'build the destination
fileDestination = "path here"
'copy the file to the new folder
FileCopy fileName, fileDestination
Next
End Function