【问题标题】:Microsoft Access 2010 open all files in a directoryMicrosoft Access 2010 打开目录中的所有文件
【发布时间】:2013-04-15 21:16:16
【问题描述】:

我正在创建一个 MS Access 2010 数据库。我正在使用 API 来执行以前版本的 MS Access 中的通用对话框控件打开目录并选择文件的操作。我的客户希望我能够在用户单击文件夹时打开目录中的所有文件(因此用户不会单击文件,而只是单击文件夹)。在使用 API 出现的通用对话框控件中单击文件夹时,我找不到偶数触发。

在 MS ACCESS 2010 中使用 API 进行通用对话框控件时,谁能告诉我如何打开目录中的所有文件(它们将是 .pdf 文件)?

我使用的 API 调用在这里:http://access.mvps.org/access/api/api0001.htm

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    使用 Microsoft.Scripting.Runtime 中的 FileSystemObject(必须添加对项目的引用)。 以下子将给定文件夹中所有 pdf 文件的字符串名称添加到集合中。 从对话框中获取文件夹路径(带有文件夹选择选项,而不是文件选择)

    Sub GetFolderPDFFiles(FolderPath As String, Col As Collection)
    
        Dim FS As New FileSystemObject
        Dim Dir As Folder
        Dim Arq As File
    
        Set Dir = FS.GetFolder(FolderPath)
    
        For Each Arq In Dir.Files
            If UCase(Right(Arq.Name, 4)) = ".PDF" Then
                Call Col.Add(Arq.Path)
            End If
        Next
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      这对我很有用...它将提示对话框选择文件夹并打开 .pdf 文件。它还将列出 Table1 中的所有文件。

          Option Compare Database
      

      '选择文件所在文件夹的功能:

          Function ChooseFolder() As String
      
      
              Dim fldr As FileDialog
              Dim sItem As String
      
              Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
              With fldr
                  .Title = "Select a Folder"
                  .AllowMultiSelect = False
                  .InitialFileName = strPath
                  If .Show <> -1 Then GoTo NextCode
                  sItem = .SelectedItems(1)
              End With
      
          NextCode:
              ChooseFolder = sItem
              Set fldr = Nothing
      
          End Function
      

      输入例程打开并列出文件夹中的 pdf 文件(它还会查找子文件夹中的文件):

          Sub Open_List_Files()
      
      
          'Declare the variables
          Dim objFSO As Scripting.FileSystemObject
          Dim objFolder, objTopFolder As Scripting.Folder
          Dim strTopFolderName As String, ProjectF As String
          Dim i As Long
      
          ' call the function to select the folder
          Call Módulo1.ChooseFolder
      
          'Create an instance of the FileSystemObject
          Set objFSO = CreateObject("Scripting.FileSystemObject")
      
      
          'Get the top folder
          Set objTopFolder = objFSO.GetFolder(ChooseFolder)
      
          'Call the RecursiveFolder routine
          Call RecursiveFolder(objTopFolder, True)
      
      
      End Sub
      
      Sub RecursiveFolder(objFolder As Scripting.Folder, IncludeSubFolders As Boolean)
      
          'Declare the variables
          Dim objFile As Object
          Dim objSubFolder As Scripting.Folder
          Dim DBStr, filepath As String
      
          'Loop through each file in the folder
          For Each objFile In objFolder.Files
          On Error Resume Next
      
          If InStr(objFile.Name, ".pdf") Then
      
          DBStr = "INSERT INTO Table1 ([File Name]) " & _
                  " VALUES (" & _
                  "'" & objFile.Name & "', " & "');"
      
          CurrentDb.Execute DBStr
      
          'open the file
          Application.FollowHyperlink objFile
      
          End If
      
          Next objFile
      
          'Loop through files in the subfolders
          If IncludeSubFolders Then
              For Each objSubFolder In objFolder.SubFolders
                  Call RecursiveFolder(objSubFolder, True)
              Next objSubFolder
          End If
      
      
      End Sub
      

      运行 Open_List_Files() 宏就可以了! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        • 2014-08-20
        相关资源
        最近更新 更多