Open files using a command button

B

brad

I would like to be able to open a .pdf or .tiff file using a command button
in access 2003, how do I do this?
 
B

brad

one other item, for each job record the file needed to be opened will be a
different file
 
J

James A. Fortune

brad said:
I would like to be able to open a .pdf or .tiff file using a command button
in access 2003, how do I do this?

For the PDF part I try reading a registry key. Then if that doesn't
work I use a local table. It's a lot of code for something so simple,
but I have not gotten around to simplifying it yet. The ExecCmd part I
use for the final "Shell" command (not shown) is from:

Microsoft KB article Q129796

The reason I went that way was that I wanted to know if I had the file
open before trying to create a new one with the same name. AFAIC, there
is no easy way to determine if a PDF file is being read by someone else
without raising and handling an error.

Here is the key I try to read:

HKEY_CLASSES_ROOT\Software\Adobe\Acrobat\Exe

In the case where that returns a constant indicating an error has
occurred, use something like the following local table:

tblAcrobatInstallPath
AIPID AutoNumber
AcrobatReaderBaseVersion Text
AcrobatReaderDefaultInstallPath Text
AcrobatReaderOtherInstallPath Text
AIPID AcrobatReaderBaseVersion AcrobatReaderDefaultInstallPath
AcrobatReaderOtherInstallPath
1 3.0 C:\ProgramFiles\Adobe\Acrobat 3.0\Reader\ Null
2 4.0 C:\ProgramFiles\Adobe\Acrobat 4.0\Reader\ Null
3 5.0 C:\ProgramFiles\Adobe\Acrobat 5.0\Reader\ Null
4 6.0 C:\ProgramFiles\Adobe\Acrobat 6.0\Reader\ Null
5 7.0 C:\ProgramFiles\Adobe\Acrobat 7.0\Reader\ Null
6 8.0 C:\ProgramFiles\Adobe\Reader 8.0\Reader\ Null
7 9.0 C:\ProgramFiles\Adobe\Reader 9.0\Reader\ Null
8 10.0 C:\ProgramFiles\Adobe\Reader 10.0\Reader\ Null
9 11.0 C:\ProgramFiles\Adobe\Reader 11.0\Reader\ Null

and code like:

Public Function GetAcrobatReaderShellPath() As String
'Note: Use HKEY_CLASSES_ROOT\Software\Adobe\Acrobat\Exe
'since a user might have Acrobat installed and uses it instead of
Acrobat Reader
Dim MyDB As DAO.Database
Dim AcroPathRS As DAO.Recordset
Dim strSQL As String
Dim lngCount As Long
Dim lngI As Long
Dim strTemp As String
Dim varDir As Variant

strTemp = GetRegistryString(HKEY_CLASSES_ROOT,
"Software\Adobe\Acrobat\Exe", "")
If strTemp = "" Or strTemp = "Error" Then
strTemp = "Error"
'The registry string didn't work so use entry from tblAcrobatInstallPath
Set MyDB = CurrentDb
strSQL = "SELECT * FROM tblAcrobatInstallPath ORDER BY AIPID DESC;"
Set AcroPathRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
If AcroPathRS.RecordCount > 0 Then
AcroPathRS.MoveLast
lngCount = AcroPathRS.RecordCount
AcroPathRS.MoveFirst
For lngI = 1 To lngCount
varDir = AcroPathRS("AcrobatReaderOtherInstallPath")
If Not IsNull(varDir) Then
If Dir(varDir & "AcroRd32.exe") <> "" Then
strTemp = varDir & "AcroRd32.exe"
Exit For
End If
Else
varDir = AcroPathRS("AcrobatReaderDefaultInstallPath")
If Not IsNull(varDir) Then
If Dir(varDir & "AcroRd32.exe") <> "" Then
strTemp = varDir & "AcroRd32.exe"
Exit For
End If
End If
End If
If lngI <> lngCount Then AcroPathRS.MoveNext
Next lngI
End If
AcroPathRS.Close
Set AcroPathRS = Nothing
Set MyDB = Nothing
End If
GetAcrobatReaderShellPath = strTemp
End Function

I modified the GetRegistryString() code to return "Error" when certain
errors occur. This entire code hack is not pretty, but it might help
you get to where you're going.

--'Support functions and constants for reading a string from the registry
'I have modified slightly the code posted by John deKrafft
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String,
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As
Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long,
ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Const SYNCHRONIZE As Long = &H100000
Public Const STANDARD_RIGHTS_ALL As Long = &H1F0000
Public Const KEY_QUERY_VALUE As Long = &H1
Public Const KEY_SET_VALUE As Long = &H2
Public Const KEY_CREATE_SUB_KEY As Long = &H4
Public Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Public Const KEY_NOTIFY As Long = &H10
Public Const KEY_CREATE_LINK As Long = &H20
Public Const KEY_ALL_ACCESS As Long = ((STANDARD_RIGHTS_ALL Or _
KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not
SYNCHRONIZE))
Public Const ERROR_SUCCESS As Long = 0
Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
Public Const HKEY_CLASSES_ROOT As Long = &H80000000
'--

Public Function GetRegistryString(lngKey As Long, strSubKey As String,
strValue As String) As String
Dim lngDataType As Long
Dim lngDataLength As Long
Dim strDataString As String
Dim lngResult As Long
Dim lngHandle As Long
Const StringLength = 150

strDataString = Space(StringLength)
lngDataType = 0
lngDataLength = CLng(StringLength)
lngResult = RegOpenKeyEx(lngKey, strSubKey, 0, KEY_ALL_ACCESS, lngHandle)
If lngResult <> ERROR_SUCCESS Then
GetRegistryString = "Error"
Exit Function
End If
lngResult = RegQueryValueEx(lngHandle, strValue, 0, lngDataType, ByVal
strDataString, lngDataLength)
If lngResult <> ERROR_SUCCESS Then
GetRegistryString = "Error"
lngResult = RegCloseKey(lngHandle)
Exit Function
End If
strDataString = Left(strDataString, lngDataLength)
'Clean the string in a way that works for either registry key option
used in GetAcrobatReaderShellPath()
'Remove a doublequote from the beginning of the string, if present
If Len(strDataString) > 0 Then
If Left(strDataString, 1) = Chr(34) Then strDataString =
Right(strDataString, Len(strDataString) - 1)
End If
'Delete an extra character if present
If Right(strDataString, 3) <> "exe" And Len(strDataString) > 0 Then
strDataString = Left(strDataString, Len(strDataString) - 1)
'Remove a doublequote from the end of the string, if present
If Len(strDataString) > 0 Then
If Right(strDataString, 1) = Chr(34) Then strDataString =
Left(strDataString, Len(strDataString) - 1)
End If
'Make sure the string ends with "exe"
If Right(strDataString, 3) = "exe" Then
GetRegistryString = Left(strDataString, lngDataLength)
Else
GetRegistryString = "Error"
End If
lngResult = RegCloseKey(lngHandle)
End Function

James A. Fortune
(e-mail address removed)
 
A

Albert D. Kallal

brad said:
I would like to be able to open a .pdf or .tiff file using a command button
in access 2003, how do I do this?


Just go:

Application.Followhyperlink "c:\my stuff\manual.pdf"

The above will open any file as if you had clicked on it.....

So, if you have a field on your current form that is a pathname to some
document, say excel in a text box called

MyDocPath

Lets assume the above is

c:\my Excel Stuff\SalesJan2009.xls

Then you go:

Application.Followhyperlink me.MyDocPath
 
A

Albert D. Kallal

Why would you write all that code?

Just use:

application.FollowHyperlink "path to any document with a legal file
extension"


I count one line of code here.....
 
J

James A. Fortune

Albert said:
Why would you write all that code?

Just use:

application.FollowHyperlink "path to any document with a legal file
extension"


I count one line of code here.....

I'm not sure what to say. I already knew about the FollowHyperlink
method, but for some inexplicable reason I have avoided using it.
Perhaps I just need to test it out enough to convince myself that it's
reliable. Normally I have a reason for almost everything I do.

James A. Fortune
(e-mail address removed)
 
B

Brad

good afternoon Albert,

I created a label called test, then in the prperties label I made the
Hyperlink address equal to C:\Temp\dwg-1.pdf using the edit hyperlink button.
when I run the form it only brings adobe into focus and does not open the
document itself.

What am I doing wrong?
 
A

Albert D. Kallal

Don't use a label or hyperlink....

Just use a plane Jane simple text box. You can have the text box connected
(bound) to a underlying field in your table....

Place a command button beside the text box (the button could be "Edit
document" or whatever).

The code behind the button will thus be:

Application.Followhyperlink me.NameOfTextBoxControlGoesHere
 
B

Brad

Thank you, I have a couple items though

Can the ms office access warning be suppressed?
After clicking the "yes" button on the warning, it only brings adobe to
focus and does not open the file itself. what am I doing wrong? I have adobe
standard (7.0) & reader (9.0) installed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top