【问题标题】:MS-ACCESS Copying file via vba with file dialogMS-ACCESS 通过带有文件对话框的 vba 复制文件
【发布时间】:2013-12-11 00:55:04
【问题描述】:

我正在尝试创建一个打开文件对话框的按钮,然后让您选择要复制到包含数据库的文件夹中的图像。我一直在使用这段代码,但我被困在 filecopy 命令上,我似乎无法正确格式化它。我使用数据库的路径加上几个文件夹,最后是一个组合框来选择特定的文件夹来创建路径(这样如果数据库被移动它就不会中断,并且组合框根据类别对图像进行排序) .这是我一直在使用的代码。谢谢各位。

Private Sub Command156_Click()

   Dim fDialog As Office.FileDialog
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Dim varFile As Variant



   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fd.InitialFileName = [Application].[CurrentProject].[Path]
   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select a Image"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.Add "All 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

     filecopy([.SelectedItems],[GetDBPath] & "\Images\Equipment\" & Combo153)

      Else

      End If
   End With
End Sub

【问题讨论】:

  • 那么问题/你的问题是什么?
  • 去掉括号([]。语法为FileCopy SourceFile, DestinationFile

标签: vba ms-access filedialog


【解决方案1】:

使用 Siddharth routs 建议,我删除了多余的括号并进行了一些调整,瞧!代码有效。我尝试了engineersmnky 方法,但路径没有正确生成。为了修复代码本身,唯一真正的错误是文件副本的目标部分没有文件名,所以我使用了

Dir(Trim(.SelectedItems.Item(1)

获取文件名并将其添加到最后。这是其他任何想要的代码的其余部分。

Private Sub Command156_Click()

   Dim fDialog As Office.FileDialog
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Dim varFile As Variant



   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fd.InitialFileName = Application.CurrentProject.Path
   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select a Image"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.Add "All 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 section takes the selected image and copy's it to the generated path'
      ' the string takes the file location, navigates to the image folder, uses the combo box selection to decide the file category, then uses the name from the filedialog to finish the path'
     FileCopy .SelectedItems(1), Application.CurrentProject.Path & "\Images\Equipment\" & Combo153 & "\" & Dir(Trim(.SelectedItems.Item(1)))


      Else

      End If
   End With
End Sub

【讨论】:

    【解决方案2】:

    我已经回答了这个问题here,但我会为你重新发布

    这是一个概念

    Sub Locate_File()
       Dim fDialog As Office.FileDialog
       Dim file_path As String
       Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    
       With fDialog  
        'Set the title of the dialog box.
        .Title = "Please select one or more files"
    
        'Clear out the current filters, and add our own.
        .Filters.Clear
        .Filters.Add "All 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
           file_path = .SelectedItems(1)
           Copy_file file_path,Combo153
        Else
           MsgBox "You clicked Cancel in the file dialog box."
        End If
      End With
    End
    
    Sub Copy_file(old_path As String, file_name As String)
      Dim fs As Object
      Dim images_path As String
      images_path = CurrentProject.Path & "\Images\Equipment\"
      Set fs = CreateObject("Scripting.FileSystemObject")
      fs.CopyFile old_path, images_path  & file_name
      Set fs = Nothing
    End
    

    您可能需要进行更改,并且必须需要 Microsoft Office 12.0 对象库才能使 FileDialog 正常工作。大部分 FileDialog 代码取自 Microsoft

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      相关资源
      最近更新 更多