从 Access VBA 以只读方式打开任意文件

发布于 2024-12-08 09:24:45 字数 572 浏览 0 评论 0原文

我有一个 Access 应用程序,当前正在使用 fhandlefile() 过程: http://access.mvps.org/access/api/api0018.htm ...打开任意文件。

我希望能够将该例程包装在我自己的过程中,例如 OpenFile(strFilePath, bolReadOnly) ,以便我可以根据需要以只读方式打开文件。但是,我在 Shell API 调用中没有看到可以执行此操作的任何地方。我已经想到的两个替代解决方案(但有问题)是:

  1. 在打开文件之前将文件属性更改为只读,然后在打开文件后立即将其更改回来。但这是一个奇怪的解决方案,因为用户必须拥有足够的权限才能执行此操作,这通常与整个“以只读方式打开文件”相矛盾。
  2. 使用与文件类型相关的 API 以只读方式打开某些类型的文件(例如 Word 文档)。这是一个问题,因为我不想区分所有不同的文件类型,并且我想尽可能远离额外的库。

有人对我如何调整现有的例程或用其他可以实现这一点的东西替代有任何想法吗?

I have an Access application that's currently using the fhandlefile() procedure here:
http://access.mvps.org/access/api/api0018.htm
...to open arbitrary files.

I'd like to be able wrap that routine in my own procedure like OpenFile(strFilePath, bolReadOnly) so that I can open files as read-only as needed. However, I don't see anywhere in that Shell API call where I could do that. Two alternative solutions I've already thought of (but have issues) are:

  1. Change the file attributes just before opening the file to be read-only, then change them back right after opening it. This is a weird solution though because the user has to have sufficient privileges in order to do this, which usually contradicts the whole "opening file as read-only" thing.
  2. Use file-type-dependent APIs to open certain types of files (Word documents for example) as read-only. This is a problem because I don't want to have to case out all different file types, and I want to stay away from extra libraries as much as possible.

Anyone have any ideas on how I can tweak my existing routine or substitute it with something else that would allow this?

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

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

发布评论

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

评论(1

記憶穿過時間隧道 2024-12-15 09:24:45

查看您正在调用的 API...

  1. 尝试使用对 ShellExecte 使用默认动词打开文件(如果可用)。如果没有,则使用“open”动词。

  2. 如果失败,它会尝试使用“打开方式...”对话框打开文件,请参阅如何使用 _shellexecute 调用“打开方式...”对话框

这两种方法都使用关联的信息使用文件打开正确的应用程序,但无法通过“只读”。我的猜测是,这是因为并非每种文件类型都有只读甚至可写上下文。

您也可以在 Web 浏览器中打开该文件。

例如(使用对“Microsoft Internet Controls”的引用,通常位于 c:\windows\system32\ieframe.dll)

Dim ie As InternetExplorer
set ie = New InternetExplorer

ie.Navigate "file://yourFile"
ie.Visible = True

Looking at the API you're calling it...

  1. Tries to open the using a call to ShellExecte to open the file with using the default verb is used, if available. If not, the "open" verb is used.

  2. If that fails it tries to open the file using the Open With... dialog see How To Invoke the "Open With..." Dialog Box Using _shellexecute

Both these methods use the information associated with the file to open the correct application, but there's no way to pass "Read-only". My guess is that this is because not every file type has a readonly or even an writeable context.

You could as an alternative open the file in a Web Browser.

For example (using a reference to "Microsoft Internet Controls" Typically found at c:\windows\system32\ieframe.dll)

Dim ie As InternetExplorer
set ie = New InternetExplorer

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