vba 如何向 transferText 命令添加“浏览文件”对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1941808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a "browse for file" dialog box to the transferText command?
提问by dmr
I'm creating a macro in MS Access that imports a CSV file into a table. I'm using the TransferText action to import the CSV string, and I want to allow the user to browse for the file that contains the CSV string. How do I show the "browse" dialog box to enable the user to choose the file?
我正在 MS Access 中创建一个宏,将 CSV 文件导入到表中。我正在使用 TransferText 操作导入 CSV 字符串,并且我希望允许用户浏览包含 CSV 字符串的文件。如何显示“浏览”对话框以允许用户选择文件?
采纳答案by dmr
回答by Tony Toews
See API: Call the standard Windows File Open/Save dialog box. However this requires VBA code.
请参阅 API:调用标准的 Windows 文件打开/保存对话框。但是,这需要 VBA 代码。
回答by David-W-Fenton
An alternative (which I would say is second choice to the API call recommended by @Tony Toews and @draice) would be to use the Application.FileDialog object. This has been part of the Office automation library for as long as VBA has been in Access, but in recent versions of Access (starting with either A2002 or A2003, I don't know which), the top-level Access Application has provided a wrapper for this object. Beware, though, that without a reference to the Office automation library, the ENUM values that show up in Intellisense can't be used without a reference (a helpful error message informs you of this and offers to create the reference). In short, if you use it's best to use it as you would any automation object with late binding, with the exception that you don't have to initialize the top-level object with Application.CreateObject, as it's already there for you to use.
另一种选择(我会说这是@Tony Toews 和@draice 推荐的API 调用的第二选择)是使用Application.FileDialog 对象。自从 VBA 出现在 Access 中以来,这一直是 Office 自动化库的一部分,但是在最近版本的 Access 中(从 A2002 或 A2003 开始,我不知道是哪个),顶级 Access 应用程序提供了一个此对象的包装器。但是请注意,如果没有对 Office 自动化库的引用,则不能在没有引用的情况下使用 Intellisense 中显示的 ENUM 值(有用的错误消息会通知您这一点并提供创建引用)。简而言之,如果您使用它,最好像使用后期绑定的任何自动化对象一样使用它,但您不必使用 Application 初始化顶级对象。
EDIT:
编辑:
@draice asks:
@draice 问:
I don't understand the following statements that you wrote: "the top-level Access Application has provided a wrapper for this object" "it's best to use it as you would any automation object with late binding"
我不明白您写的以下语句:“顶级访问应用程序为此对象提供了一个包装器”“最好像使用任何具有后期绑定的自动化对象一样使用它”
Tony's API code will work in every version of Windows, and because MS believes in backward compatibility, they will never break this API call in future versions of Windows.
the FileDialog object is not easy to use in VBA unless you add the reference to the Office Automation library. It is better to minimize the number of references in your Access database, because all sorts of things can mess them up and cause your app to break (any missing reference will prevent all VBA code from running). In order to avoid problems from missing references, we use late binding so that the code you write is not dependent on the outside libary.
Microsoft might remove this object from future versions of Access. The FileSearch object is an analogous situation, in that it was introduced in A95/97 as part of the Office Automation library, and then a wrapper around it was created in A2000, but Microsoft removed it in A2007 (and provided no alternative at all). MS could choose to remove the FileDialog object in future versions of Access and then your code would break. But the API call is never going to break.
Tony 的 API 代码将适用于每个版本的 Windows,并且因为 MS 相信向后兼容性,他们将永远不会在 Windows 的未来版本中破坏这个 API 调用。
FileDialog 对象在 VBA 中不容易使用,除非您添加对 Office 自动化库的引用。最好尽量减少 Access 数据库中的引用数量,因为各种各样的事情都会把它们弄乱并导致您的应用程序中断(任何缺失的引用都会阻止所有 VBA 代码运行)。为了避免缺少引用的问题,我们使用后期绑定,这样您编写的代码就不会依赖于外部库。
Microsoft 可能会从 Access 的未来版本中删除此对象。FileSearch 对象是一种类似的情况,因为它在 A95/97 中作为 Office 自动化库的一部分引入,然后在 A2000 中创建了它的包装器,但 Microsoft 在 A2007 中删除了它(并且根本没有提供替代方案) . MS 可以选择在未来版本的 Access 中删除 FileDialog 对象,然后您的代码就会中断。但是 API 调用永远不会中断。