Link to home
Start Free TrialLog in
Avatar of cogc_it
cogc_it

asked on

Access - Open attachment programmatically

I have an attachment field in my form.  I would like for the user to be able to double click it and open in its default program (ex. - Word doc opens in Word, image opens in default photo viewer, etc.).  This can be accomplished by double clicking > the Manage Attachment dialog appears > select attachment > Open.  I want the series of clicks to be limited to double click > Open.  Any ideas on how to accomplish this?

ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tried the ShellExecute API method?

See here...
http://www.mvps.org/access/api/api0018.htm
Avatar of cogc_it
cogc_it

ASKER

Thank you for the suggestions, but I was hoping to piggyback on the same logic that Access exhibits to match the file type in the attachment field with the user's default program to open it.  For example, if the attachment is an image, it opens in the user's default image viewer.  

The first suggestions involves first saving the file to a temp directory, which is not desirable.  The second option might work but I don't know enough about the attachment DAO to pass it to the function for opening (and don't really want to know - hope that makes sense).

Thanks for the suggestions.
The link I posted is the Microsoft Access Team's solution to your exact problem. In other words, the folks who WRITE Access say this is the only way to achieve that. Therefore, it is the only way to do what you ask.
Avatar of cogc_it

ASKER

I didn't see anything in that blog post about "this is the only way" but thanks for your feedback.
"The second option might work but I don't know enough about the attachment DAO to pass it to the function for opening (and don't really want to know - hope that makes sense). "

Well no that does not really make much sense at all. If you don't want to know how to use Access then why are you using it? You have been provided two solutions and seem not to want the bother of trying either.

To use the shell execute API all you need to to is copy the code in the link provided and drop it into a standard Access module.

You will then be able to open any file that windows knows about by calling fHandleFile like this...

fHandleFile sttFullPathToTheFileToOpen,1
While I share LamberHeenan's objection, I don't see how ShellExecute would work with the Attachment datatype in Access. Attachments are stored in a OLE field in Access, in a structured table arrangement which is accessed via the methods I describe at the links.

I realize the author may not know much about the inner workings of Access, but the method that I provided at the link is the way you get to Attachment fields via VBA code. You cannot do as the author requests, i.e. Doubleclick and then Open. Access Attachment fields don't work that way, since you MUST move through the table hiearchy in order to get to those hidden tables.