Access 2010 64 位有“打开文件”对话框吗?

发布于 2024-10-13 03:51:58 字数 88 浏览 18 评论 0原文

如何获得 Access 2010 64 位的“打开文件”对话框?通常我会使用通用对话框控件,但它是 32 位的,不能与 Access 2010 64 位一起使用。

How do I get an Open File dialog for Access 2010 64bit? Normally I would use the common dialog control but that is 32bit and cannot be used with Access 2010 64 bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

久隐师 2024-10-20 03:51:58

您可以使用内置的文件对话框。它自 access 2003 起就存在。

Dim f    As FileDialog 
Set f = Application.FileDialog(msoFileDialogFilePicker) 
f.Show 
MsgBox "file choose was " & f.SelectedItems(1) 

如果您愿意,可以后期绑定:

以上需求:Microsoft Office 14.0 对象库

如果删除对 14.0 对象库的引用,则以下内容
代码无需任何引用即可工作:

Dim f    As Object 
Set f = Application.FileDialog(3) 
f.AllowMultiSelect = True 
f.Show 

MsgBox "file choosen = " & f.SelectedItems.Count 

因此,上述代码适用于 2003 年以后的运行时版或普通版,也适用于 32 或 64 位版本的 access 2010。

You can use the built in file dialog. It been there since access 2003.

Dim f    As FileDialog 
Set f = Application.FileDialog(msoFileDialogFilePicker) 
f.Show 
MsgBox "file choose was " & f.SelectedItems(1) 

You can late bind if you wish:

above needs: Microsoft Office 14.0 Object library

If you remove the reference to the 14.0 object library, then the following
code will work without any references:

Dim f    As Object 
Set f = Application.FileDialog(3) 
f.AllowMultiSelect = True 
f.Show 

MsgBox "file choosen = " & f.SelectedItems.Count 

So, above works in runtime or regular edition from 2003 onwards, and also works for either 32 or 64 bit editions of access 2010.

为你鎻心 2024-10-20 03:51:58

我从未使用过打开文件对话框的控件,因为无论如何它只是 API 调用的包装器。
调用标准 Windows 文件打开/保存对话框 此外,还可以进行分发和保存控件的版本控制问题,因此我尽力避免它们。

I've never used a control for the open File dialog as it's just a wrapper for the API call anyhow.
Call the standard Windows File Open/Save dialog box In addition there can be distribution and versioning problems with controls so I do my best to avoid them.

偏爱自由 2024-10-20 03:51:58

我花了很长一段时间来解决这个问题......

你上面所说的一切都有效,但还有最后一点要添加......在WITH OFN声明下,你需要更改

.lStructSize = Len(ofn)

.lStructSize = LenB(ofn)

然后一切正常。

I was working with this problem for a long while...

Everything you have said above works but there is one last piece to add ... under the WITH OFN declaration you need to change

.lStructSize = Len(ofn)

to

.lStructSize = LenB(ofn)

And then everything works.

隔岸观火 2024-10-20 03:51:58

这个人有一个工具,可以生成与 64 位兼容的打开文件的代码。它是免费软件。

http://www.avenius.de/en/index.php?产品:IDBE_Tools

这是唯一有效的方法。

This guy has a tool that generates code that is 64 bit compatible for opening a file. It is freeware.

http://www.avenius.de/en/index.php?Products:IDBE_Tools

This was the only thing that worked.

不回头走下去 2024-10-20 03:51:58

首先,“CommonDialog Class”似乎无法在 32 位版本的 Office 上运行。它给出了相同的 OleDb 错误。正如一位评论者指出的那样,这不是您应该使用的控件。虽然您可能可以使用另一个 ActiveX 控件,但实际上并不能保证它在您想要部署数据库的每台计算机上都可用。我的开发盒上装有 Visual Studio 6、VS 2008 和 VS 2010,此外还有 Office 和其他程序,所有这些程序都提供了典型用户不可能拥有的 ActiveX DLL。此外,其中许多库不可重新分发,或者造成独特的安装障碍,可能根本不值得这么麻烦。

到目前为止,最简单、最通用的解决方案是从 Windows API 调用“打开”对话框。它位于 comdlg32.dll 中,该文件在您可能瞄准的每个 Windows 版本上都可用,并且不不对 comdlg32.ocx 施加任何依赖。它还提供比使用 ActiveX 控件更好的性能,因为它不需要将附加模块加载到内存中。

所需的代码也不是很复杂。您需要提供函数 GetOpenFileName,这会创建“打开”对话框。它采用单个参数,即 OPENFILENAME 结构的实例 包含用于初始化对话框的信息,以及接收用户选择的文件的路径。因此,您还需要提供此结构的声明。 VBA 中的代码看起来像这样:

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (ByRef lpofn As OPENFILENAME) As Long

您还可以将几个常量作为标志传递来自定义对话框的行为。为了完整起见,这里是完整列表:

Private Const OFN_ALLOWMULTISELECT As Long = &H200
Private Const OFN_CREATEPROMPT As Long = &H2000
Private Const OFN_ENABLEHOOK As Long = &H20
Private Const OFN_ENABLETEMPLATE As Long = &H40
Private Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
Private Const OFN_EXPLORER As Long = &H80000
Private Const OFN_EXTENSIONDIFFERENT As Long = &H400
Private Const OFN_FILEMUSTEXIST As Long = &H1000
Private Const OFN_HIDEREADONLY As Long = &H4
Private Const OFN_LONGNAMES As Long = &H200000
Private Const OFN_NOCHANGEDIR As Long = &H8
Private Const OFN_NODEREFERENCELINKS As Long = &H100000
Private Const OFN_NOLONGNAMES As Long = &H40000
Private Const OFN_NONETWORKBUTTON As Long = &H20000
Private Const OFN_NOREADONLYRETURN As Long = &H8000&
Private Const OFN_NOTESTFILECREATE As Long = &H10000
Private Const OFN_NOVALIDATE As Long = &H100
Private Const OFN_OVERWRITEPROMPT As Long = &H2
Private Const OFN_PATHMUSTEXIST As Long = &H800
Private Const OFN_READONLY As Long = &H1
Private Const OFN_SHAREAWARE As Long = &H4000
Private Const OFN_SHAREFALLTHROUGH As Long = 2
Private Const OFN_SHAREWARN As Long = 0
Private Const OFN_SHARENOWARN As Long = 1
Private Const OFN_SHOWHELP As Long = &H10
Private Const OFS_MAXPATHNAME As Long = 260

为了方便起见,我将整个混乱的内容包装在一个辅助函数中,您可以从 VBA 中调用该函数。它接受您最常需要为打开文件对话框设置的属性作为参数,处理调用 Windows API 本身,然后返回用户所选文件的完整路径或空字符串 (vbNullString)如果用户单击“取消”按钮。您可以测试调用代码中的返回值以确定要采取的操作过程。

'This function shows the Windows Open File dialog with the specified
' parameters, and either returns the full path to the selected file,
' or an empty string if the user cancels.
Public Function OpenFile(ByVal Title As String, ByVal Filter As String, _
    ByVal FilterIndex As Integer, ByVal StartPath As String, _
    Optional OwnerForm As Form = Nothing) As String

    'Create and populate an OPENFILENAME structure
    'using the specified parameters
    Dim ofn As OPENFILENAME
    With ofn
        .lStructSize = Len(ofn)
        If OwnerForm Is Nothing Then
            .hwndOwner = 0
        Else
            .hwndOwner = OwnerForm.Hwnd
        End If
        .lpstrFilter = Filter
        .nFilterIndex = FilterIndex
        .lpstrFile = Space$(1024) & vbNullChar & vbNullChar
        .nMaxFile = Len(ofn.lpstrFile)
        .lpstrFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
        .nMaxFileTitle = Len(.lpstrFileTitle)
        .lpstrInitialDir = StartPath & vbNullChar & vbNullChar
        .lpstrTitle = Title
        .flags = OFN_FILEMUSTEXIST
    End With

    'Call the Windows API function to show the dialog
    If GetOpenFileName(ofn) = 0 Then
        'The user pressed cancel, so return an empty string
        OpenFile = vbNullString
    Else
        'The user selected a file, so remove the null-terminators
        ' and return the full path
        OpenFile = Trim$(Left$(ofn.lpstrFile, Len(ofn.lpstrFile) - 2))
    End If
End Function

哇,结果很长。您需要将大量声明复制并粘贴到模块中,但您实际需要处理的接口却出奇地简单。以下是如何在代码中实际使用它来显示打开文件对话框并获取文件路径的示例:

Public Sub DoWork()
    'Set the filter string (patterns) for the open file dialog
    Dim strFilter As String
    strFilter = "Text Files (*.txt)" & vbNullChar & "*.txt*" & vbNullChar & _
                "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar

    'Show the open file dialog with the custom title, the filters specified
    ' above, and starting in the root directory of the C: drive.
    Dim strFileToOpen As String
    strFileToOpen = OpenFile("Choose a file to open", strFilter, 0, "C:\")

    'See if the user selected a file
    If strFileToOpen = vbNullString Then
        MsgBox "The user pressed the Cancel button."
    Else
        MsgBox "The user chose to open the following file: " & _
               vbNewLine & strFileToOpen 
    End If
End Sub

编写和测试此解决方案的最长部分实际上是试图找到如何打开VBA 编辑器并在 Access 中编写宏。对于使用主要菜单“粘贴”和“保存”的人来说,功能区可能是一项伟大的发明,但这是多么痛苦的事情。我花了一整天的时间使用软件,但我仍然找不到东西。 [/咆哮]

First of all, the "CommonDialog Class" doesn't even appear to work on a 32-bit version of Office. It gives the same OleDb error. As one of the commenters points out, this isn't the control you should be using. And while there might be another ActiveX control you could use, there's really no guarantee that it will be available on every machine that you want to deploy your database on. My dev box has Visual Studio 6, VS 2008, and VS 2010 on it, in addition to Office and other programs, all of which provide ActiveX DLLs that a typical user could not be expected to have. Additionally, many of these libraries are not redistributable, or pose unique installation hurdles that may simply not be worth the trouble.

By far, the simplest, most universal solution is to call the Open dialog from the Windows API. It's located in comdlg32.dll, which is available on every version of Windows you could possibly be targeting, and doesn't impose any dependencies on comdlg32.ocx. It also provides better performance than using an ActiveX control because it doesn't require an additional module to be loaded into memory.

The code that is required isn't very complicated either. You need to provide a declaration for the function GetOpenFileName, which creates the Open dialog box. It takes a single parameter, an instance of the OPENFILENAME structure that contains information used to initialize the dialog box, as well as receiving the path to the file selected by the user. So you'll also need to provide a declaration of this structure. The code in VBA would look something like this:

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (ByRef lpofn As OPENFILENAME) As Long

There are also a couple of constants you can pass as flags to customize the dialog's behavior. For completeness, here's the full list:

Private Const OFN_ALLOWMULTISELECT As Long = &H200
Private Const OFN_CREATEPROMPT As Long = &H2000
Private Const OFN_ENABLEHOOK As Long = &H20
Private Const OFN_ENABLETEMPLATE As Long = &H40
Private Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
Private Const OFN_EXPLORER As Long = &H80000
Private Const OFN_EXTENSIONDIFFERENT As Long = &H400
Private Const OFN_FILEMUSTEXIST As Long = &H1000
Private Const OFN_HIDEREADONLY As Long = &H4
Private Const OFN_LONGNAMES As Long = &H200000
Private Const OFN_NOCHANGEDIR As Long = &H8
Private Const OFN_NODEREFERENCELINKS As Long = &H100000
Private Const OFN_NOLONGNAMES As Long = &H40000
Private Const OFN_NONETWORKBUTTON As Long = &H20000
Private Const OFN_NOREADONLYRETURN As Long = &H8000&
Private Const OFN_NOTESTFILECREATE As Long = &H10000
Private Const OFN_NOVALIDATE As Long = &H100
Private Const OFN_OVERWRITEPROMPT As Long = &H2
Private Const OFN_PATHMUSTEXIST As Long = &H800
Private Const OFN_READONLY As Long = &H1
Private Const OFN_SHAREAWARE As Long = &H4000
Private Const OFN_SHAREFALLTHROUGH As Long = 2
Private Const OFN_SHAREWARN As Long = 0
Private Const OFN_SHARENOWARN As Long = 1
Private Const OFN_SHOWHELP As Long = &H10
Private Const OFS_MAXPATHNAME As Long = 260

And for convenience, I've wrapped this whole mess inside of a helper function that you can call from within VBA. It accepts as parameters the properties you will most commonly need to set for the open file dialog, handles calling the Windows API itself, and then returns either the full path to the file selected by the user, or an empty string (vbNullString) if the user clicked the Cancel button. You can test the return value in the calling code to determine which course of action to take.

'This function shows the Windows Open File dialog with the specified
' parameters, and either returns the full path to the selected file,
' or an empty string if the user cancels.
Public Function OpenFile(ByVal Title As String, ByVal Filter As String, _
    ByVal FilterIndex As Integer, ByVal StartPath As String, _
    Optional OwnerForm As Form = Nothing) As String

    'Create and populate an OPENFILENAME structure
    'using the specified parameters
    Dim ofn As OPENFILENAME
    With ofn
        .lStructSize = Len(ofn)
        If OwnerForm Is Nothing Then
            .hwndOwner = 0
        Else
            .hwndOwner = OwnerForm.Hwnd
        End If
        .lpstrFilter = Filter
        .nFilterIndex = FilterIndex
        .lpstrFile = Space$(1024) & vbNullChar & vbNullChar
        .nMaxFile = Len(ofn.lpstrFile)
        .lpstrFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
        .nMaxFileTitle = Len(.lpstrFileTitle)
        .lpstrInitialDir = StartPath & vbNullChar & vbNullChar
        .lpstrTitle = Title
        .flags = OFN_FILEMUSTEXIST
    End With

    'Call the Windows API function to show the dialog
    If GetOpenFileName(ofn) = 0 Then
        'The user pressed cancel, so return an empty string
        OpenFile = vbNullString
    Else
        'The user selected a file, so remove the null-terminators
        ' and return the full path
        OpenFile = Trim$(Left$(ofn.lpstrFile, Len(ofn.lpstrFile) - 2))
    End If
End Function

Wow that ended up being long. There are a lot of declarations you'll need to copy and paste into a module, but the interface you actually have to deal with is surprisingly simple. Here's a sample of how you might actually use this in your code to show the open file dialog and get the path to a file:

Public Sub DoWork()
    'Set the filter string (patterns) for the open file dialog
    Dim strFilter As String
    strFilter = "Text Files (*.txt)" & vbNullChar & "*.txt*" & vbNullChar & _
                "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar

    'Show the open file dialog with the custom title, the filters specified
    ' above, and starting in the root directory of the C: drive.
    Dim strFileToOpen As String
    strFileToOpen = OpenFile("Choose a file to open", strFilter, 0, "C:\")

    'See if the user selected a file
    If strFileToOpen = vbNullString Then
        MsgBox "The user pressed the Cancel button."
    Else
        MsgBox "The user chose to open the following file: " & _
               vbNewLine & strFileToOpen 
    End If
End Sub

The longest part of writing and testing this solution was actually trying to find how to open the VBA editor and write a macro in Access. The Ribbon might be a great invention for people who use the menu primary for "Paste" and "Save", but what a pain. I spend all day using software, and I still can't find stuff. [/rant]

孤独难免 2024-10-20 03:51:58

我错过了 64 位访问详细信息。您不太可能应该运行它,但如果您运行它,这里有一篇文章供您考虑,其中解释了如何更改 API 调用才能工作 - 您必须使用新的长指针数据类型:

Office 2010 32 位和 64 位版本之间的兼容性

如果您相应地更改 API 代码,它应该可以在 64 位 Access 上正常工作。

但您确实应该问一下为什么要使用 64 位 Access。 MS 实际上根本不建议任何人使用 64 位 Office,除非他们有需要它的具体原因(例如需要使用它提供的额外内存,特别是对于复杂的 Excel 电子表格模型之类的东西)。 Access 绝对不是从转换到 64 位中受益匪浅的应用程序之一。

详细讨论主题:

简而言之,大多数人不应该运行 64 位 Office,这正是您遇到的原因 - 它会导致外部依赖于 32 位组件和 API 的遗留代码失败。

I missed the 64-bit Access detail. It's very unlikely that you should be running it, but if you are, here is an article for your consideration that explains how you have to alter your API call to work -- you have to use the new long pointer data type:

Compatibility Between the 32-bit and 64-bit Versions of Office 2010

If you alter the API code accordingly, it should work fine on 64-bit Access.

But you should really ask why you're using 64-bit Access. It's really not at all recommended by MS that anyone use 64-bit Office unless they have specific reasons why they need it (such as needing to use the extra memory it provides, particularly for things like complex Excel spreadsheet models). Access is definitely not one of the apps that benefits much from the conversion to 64-bit.

Detailed discussion of the subject:

In short, most people shouldn't be running 64-bit Office, precisely for the reason you encountered -- it causes legacy code with outside dependencies on 32-bit components and APIs to fail.

别靠近我心 2024-10-20 03:51:58

我刚刚在 64 位版本的 Excel 2013 中努力解决此问题。

结合...

  1. 对其中 3 个项目使用 LongPtr 数据类型 (hwndOwner传递给 GetOpenFileNameAOPENFILENAME 结构中的 code>、hInstancelpfnHook
  2. 替换 Len在获取 OPENFILENAME 结构的大小时(如 Max Albanese 提到的),使用 LenB 函数使用 code> 函数

...成功了,这要归功于此处记录的指导:
https://gpgonaccess。 blogspot.co.uk/2010/03/work-in-progress-and-64-bit-vba.html

I've just been wrestling with resolving this issue in a 64-bit version of Excel 2013.

A combination of...

  1. Using the LongPtr data type for 3 of the items (hwndOwner, hInstance, lpfnHook) in the OPENFILENAME structure passed to GetOpenFileNameA
  2. Replacing the Len function with the LenB function when obtaining the size of the OPENFILENAME structure (as mentioned by Max Albanese)

...did the trick, thanks to guidance documented here:
https://gpgonaccess.blogspot.co.uk/2010/03/work-in-progress-and-64-bit-vba.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文