Hyperlink in Access to Word Document (1 Viewer)

PeteB

Registered User.
Local time
Today, 04:16
Joined
Mar 15, 2016
Messages
74
Hi All

I have an Access database within which two of its forms, apart from the fields you would expect to see, each have a multitude of hyperlinks, some linked to items within the database and some to either Word documents or templates.

Two of the Word templates contain fill-in fields, and when one of these templates is called by double clicking on it in Windows Explorer i.e. ‘File New’, dialog boxes open in sequence, one for each fill-in field.

Attempting to hyperlink these templates on an Access form, and clicking on the hyperlink, results in the template being opened i.e. ‘File Open’ which opens the template as if I wish to modify it but the fill-in dialog boxes are not called.

I appreciate that I could create the document within an Access report, but there must be a way, I would have thought, of opening the required document from the Word template via a hyperlink rather than opening the template itself. My Access experience unfortunately does not stretch that far.

Can anyone help?

PeteB
 

Ranman256

Well-known member
Local time
Today, 07:16
Joined
Apr 9, 2015
Messages
3,891
Store the path to the file in the field, and it will open ANY file in its native application.
Paste this code into a module, then its usage is: OpenNativeApp txtBox

OpenNativeApp "c:\folder\file.dot"
will open the template in word

but it will work for all different types,
OpenNativeApp "c:\folder\"
will open the folder explorer

OpenNativeApp "c:\folder\file.pdf"
will open in acrobat

Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, msg As String
r = StartDoc(psDocName)
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = "File not found"
Case SE_ERR_PNF
msg = "Path not found"
Case SE_ERR_ACCESSDENIED
msg = "Access denied"
Case SE_ERR_OOM
msg = "Out of memory"
Case SE_ERR_DLLNOTFOUND
msg = "DLL not found"
Case SE_ERR_SHARE
msg = "A sharing violation occurred"
Case SE_ERR_ASSOCINCOMPLETE
msg = "Incomplete or invalid file association"
Case SE_ERR_DDETIMEOUT
msg = "DDE Time out"
Case SE_ERR_DDEFAIL
msg = "DDE transaction failed"
Case SE_ERR_DDEBUSY
msg = "DDE busy"
Case SE_ERR_NOASSOC
msg = "No association for file extension"
Case ERROR_BAD_FORMAT
msg = "Invalid EXE file or error in EXE image"
Case Else
msg = "Unknown error"
End Select
' MsgBox msg
End If
End Sub
 

PeteB

Registered User.
Local time
Today, 04:16
Joined
Mar 15, 2016
Messages
74
Thanks Ranman, I'll give it a try.
PeteB
 

Isaac

Lifelong Learner
Local time
Today, 04:16
Joined
Mar 14, 2017
Messages
5,386
Store the path to the file in the field, and it will open ANY file in its native application.
Paste this code into a module, then its usage is: OpenNativeApp txtBox

OpenNativeApp "c:\folder\file.dot"
will open the template in word

but it will work for all different types,
OpenNativeApp "c:\folder\"
will open the folder explorer

OpenNativeApp "c:\folder\file.pdf"
will open in acrobat

Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, msg As String
r = StartDoc(psDocName)
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = "File not found"
Case SE_ERR_PNF
msg = "Path not found"
Case SE_ERR_ACCESSDENIED
msg = "Access denied"
Case SE_ERR_OOM
msg = "Out of memory"
Case SE_ERR_DLLNOTFOUND
msg = "DLL not found"
Case SE_ERR_SHARE
msg = "A sharing violation occurred"
Case SE_ERR_ASSOCINCOMPLETE
msg = "Incomplete or invalid file association"
Case SE_ERR_DDETIMEOUT
msg = "DDE Time out"
Case SE_ERR_DDEFAIL
msg = "DDE transaction failed"
Case SE_ERR_DDEBUSY
msg = "DDE busy"
Case SE_ERR_NOASSOC
msg = "No association for file extension"
Case ERROR_BAD_FORMAT
msg = "Invalid EXE file or error in EXE image"
Case Else
msg = "Unknown error"
End Select
' MsgBox msg
End If
End Sub
That code gives a compile error on StartDoc, which apparently is some external function?
 

Ranman256

Well-known member
Local time
Today, 07:16
Joined
Apr 9, 2015
Messages
3,891
its not protected for 64bit. Add PTRSAFE to the function declares.
I will fix in future.

Code:
#If Win64 Then      'Public Dclare PtrSafe Function
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long

#else
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
#End If
 

Isaac

Lifelong Learner
Local time
Today, 04:16
Joined
Mar 14, 2017
Messages
5,386
its not protected for 64bit. Add PTRSAFE to the function declares.
I will fix in future.
No, had nothing to do with that. Has an external function. I already fixed ptrsafe
 

Users who are viewing this thread

Top Bottom