0

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

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

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

4

2 回答 2

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
于 2013-04-15T21:22:01.387 回答
0

这对我很有用......它会提示对话框选择文件夹并打开 .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() 宏就可以了!:)

于 2018-01-30T13:08:59.433 回答