Excel VBA - Right Click File Browser to Create Link To Locally Stored .JPGs

M22RDY

New Member
Joined
Oct 3, 2006
Messages
26
Hi,

I've been playing about with this for ages, I have row 'U' which I manually link to local JPG files. Basically it has the word "HERE" and linked to a JPG that opens when you click it.

I'm really looking to just RIGHT CLICK a cell in column U, it opens a file browser, when I select the file it places HERE in the cell and links it to the file I've selected.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi patel45,

Unfortunately, the sample file contains a lot of sensitive names/addresses as well as financial info.

But the basic premise is I wish to link to a scanned jpg image file just by right clicking on a cell (in row U) and selecting the file, which in turn automatically creates the link.
 
Upvote 0
[SOLVED]

Here is the solution for anyone stumbling over this thread:

Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    
    ' Retreive cell column number
    cell_check = ActiveCell.Column

    If cell_check = 21 Then 'checks if Column 21 (U) is selected
        
            Dim file_pick As Long
 
            ' Open the file dialog
            With Application.FileDialog(msoFileDialogOpen)
                .AllowMultiSelect = False
                .Show
 
            ' Display paths of each file selected
            For file_pick = 1 To .SelectedItems.Count
                file_address = .SelectedItems(file_pick)
            Next file_pick
 
            End With
    
    ' Created hyperlink and adds HERE text
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=file_address, TextToDisplay:="HERE"
    
    'Disables Right Click menu
    Cancel = True
    
    End If

End Sub
 
Upvote 0
There is a strange anomaly happening when I use the above mentioned method.

Initially, when the file is hyper linked, it creates an absolute reference to the file and opens perfectly

Code:
Before save, close & re-open
file:///E:\main_sub_directory\sub_directory\file location

Now, the strange thing happens when I save the file and close it, the XLSM is located within the main subdirectory. After it's saves and opened, the link has the main sub directory removed but everything else intact.

Code:
After save, close & re-open
file:///E:\sub_directory\file location

Thus it screws up the links and throws 'cannot open selected file' error because it has wiped the main directory (the folder reference) from the hyperlink.

Is there a way to keep the absolute path after a save and re-open?
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top