Adding a clickable link by using a Browse button

GrahamUK33

Registered User.
Local time
Today, 06:20
Joined
May 19, 2011
Messages
58
I have added a browse button that inserts a link address to a file on our network. The only problem I have with it is that when the link is clicked on the file does not open up.

When a website address is saved, it can be clicked on and the website will open up.

I am wondering if there needs to be a character inserted before the network address to make the link clickable.

The code I have is as follows:

Code:
Private Sub cmdBrowse_Click()
    Dim File As Variant
    File = GetFile()
    If IsNull(File) Then
        MsgBox "Nothing was selected", vbOKOnly
    Else
        Me.URL = File
    End If
End Sub

Public Function GetFile() As Variant
    Dim dialog As Object
    Dim pickedfile As Boolean
    Set dialog = Application.FileDialog(3)
    GetFile = Null
    With dialog
        .AllowMultiSelect = False
        .Title = "Please select file for import"
        .Filters.Clear
like to browse for all files
        pickedfile = False
        pickedfile = .Show
        If pickedfile Then
            GetFile = .SelectedItems.Item(1)
        End If
    End With
End Function
 
paste this code into a module. (Alt-F11, insert , module)
then it will open ANY file via its extension....

.pdf files will open in acrobat,
.doc files in word
web sites open in web browser
file folders open in file browser
etc

USAGE:
Code:
File = GetFile()
    If IsNull(File) Then
        MsgBox "Nothing was selected", vbOKOnly
    Else
         OpenNativeApp File      
    End If

Code:
Option Compare Database
Option Explicit

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

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
 
I still have the same problem where the link in not clickable.

I did find that the link needs to be in-between " " any idea how I can do that with the code that I posted?
 
If you use the Application.Followhyperlink method, you can open ANY file as long as the extension is registered.
 
The code that I have works how we want it to, apart from the link not being clickable. Can the code below be amended to include speech marks either-side of the link to make the link clickable?

"link"

Code:
Private Sub cmdBrowse_Click()
    Dim File As Variant
    File = GetFile()
    If IsNull(File) Then
        MsgBox "Nothing was selected", vbOKOnly
    Else
        Me.URL = File
    End If
End Sub

Public Function GetFile() As Variant
    Dim dialog As Object
    Dim pickedfile As Boolean
    Set dialog = Application.FileDialog(3)
    GetFile = Null
    With dialog
        .AllowMultiSelect = False
        .Title = "Please select file for import"
        .Filters.Clear
like to browse for all files
        pickedfile = False
        pickedfile = .Show
        If pickedfile Then
            GetFile = .SelectedItems.Item(1)
        End If
    End With
End Function
 

Users who are viewing this thread

Back
Top Bottom