Microsoft Access 2010打开目录中的所有文件

时间:2013-04-15 21:16:17

标签: access-vba

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

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

我正在使用的API调用位于:http://access.mvps.org/access/api/api0001.htm

2 个答案:

答案 0 :(得分:0)

使用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

答案 1 :(得分:0)

这对我来说非常有用......它将提示对话框选择文件夹并打开.pdf文件。它还将列出表1中的所有文件。

    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()宏,然后你去! :)