【问题标题】:Save attached files from list box in Access?从 Access 的列表框中保存附件?
【发布时间】:2013-08-14 19:23:37
【问题描述】:

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

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

【问题讨论】:

    标签: vba ms-access-2010 filepath


    【解决方案1】:

    我想这就是你要找的东西:

    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 给了我一个错误。它现在在 Me.InvisiblePathList.List(i) 上给我一个错误。 .List(i) 的目的是什么?
    • 没关系,我相信我已经想通了。感谢您为我指明正确的方向@tigeravatar。这是我修改的内容:
    • '将此更新到正确的文件夹,确保包含结尾 \fileDestination = "myPathHere" For i = 0 To Me.FileList.ListCount - 1 FileCopy Me.InvisiblePathList.ItemData(i) & "\" & Me.FileList.ItemData(i), fileDestination & Me.FileList.ItemData(i) Next i
    • 很高兴你能成功!我已经编辑了答案以反映您的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多