Hyperlink subaddress (1 Viewer)

Denise2020

Member
Local time
Today, 17:14
Joined
Mar 31, 2020
Messages
82
Good afternoon all,

I have a form with a "Reference" text box with a button. The button currently has VBA to open a specific folder where files can be found for each record as default, if the text box is empty. This part works without any problems.

What I would like to happen though, is if a specific file is typed into the reference text box, that it searches in that folder for the specific file and opens it.

My simple VBA code is:

Private sub btnOpenRef_Click()
Application.FollowHyperlink "... specific folder location in explorer listed here" & Me.Referens
End Sub

Private sub btnOpenRef_Click()
Application.FollowHyperlink "... specific folder location in explorer listed here" & Me![Referens]
End Sub


I have tried tacking on the text box at the end as show in red, but so far not found something that works. Is there a way to do this? Thank you so much for the help.
 

Ranman256

Well-known member
Local time
Today, 11:14
Joined
Apr 9, 2015
Messages
4,337
This code has many uses.
Paste this code into a module,
Store the path in the field, and it will open ANY file in its native application.

if the item in the text box is a URL, it will open in default explorer

if .docx file, it will open the document in word
if .xlsx file, it will open the document in excel

it reads the path in the textbox
its usage is: OpenNativeApp txtBox



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


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


Private Function StartDoc(psDocName As String) As Long
Dim Scr_hDC As Long

Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function
 

Users who are viewing this thread

Top Bottom