Open Arbitrary File from Access VBA as Read-Only
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:
- 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.
- 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 开发者_如何学GoI can tweak my existing routine or substitute it with something else that would allow this?
Looking at the API you're calling it...
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.
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
精彩评论