Vba 是否从Access中的列表框保存附件?

Vba 是否从Access中的列表框保存附件?,vba,ms-access-2010,filepath,Vba,Ms Access 2010,Filepath,我有一个允许用户添加文件的表单和一个列出这些添加文件的列表框: 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

我有一个允许用户添加文件的表单和一个列出这些添加文件的列表框:

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

我想这就是你想要的:

Sub SaveAttachments()

    Dim fileDestination As String
    Dim i As Long

    'Update this to the correct folder, be sure to include the ending \
    fileDestination = "Drive:\Path\To\Folder\"

    For i = 0 To Me.FileList.ListCount - 1
        FileCopy Me.InvisiblePathList.ItemData(i) & Application.PathSeparator & Me.FileList.ItemData(i), fileDestination & Me.FileList.ItemData(i)
    Next i

End Sub

我可能应该提到我正在使用Access 2010。我必须将Me.FileList.Count更改为Me.FileList.ListCount,因为Access给了我一个错误。现在它给了我一个错误。InvisibleMathlist.List(i)。列表(i)的目的是什么?没关系,我相信我已经弄明白了。谢谢你为我指明了正确的方向@tigeravatar。下面是我修改的内容:'将其更新到正确的文件夹,确保将I=0的结尾\fileDestination=“myPathHere”包含到Me.FileList.ListCount-1 FileCopy Me.InvisibleThrist.ItemData(I)&“\”&Me.FileList.ItemData(I),fileDestination&Me.FileList.ItemData(I)下一步我很高兴它能正常工作!我编辑了答案以反映您的解决方案:)