Attach hyperlink to an image

DarkRevs

New Member
Joined
Oct 25, 2018
Messages
2
Hi all,

Thank you for taking the time to view my question.

I am looking for a way to attach the hyperlink which i have in my clipboard to an image i add with this code.

Is there any one who can help me out with the last part?

thanks!

Code:
Private Sub InsertWord_Click()


Dim clipboardData As New DataObject 'get the info from clipboard
Dim clipboardContents As String 'get the info from clipboard
clipboardData.GetFromClipboard 'get the info from clipboard
clipboardContents = clipboardData.GetText 'get the info from clipboard
 
Dim clLeft As Double 'select the active cell
Dim clTop As Double 'select the active cell
Dim sFile As String 'select the picture to be added
sFile = "C:\Insert Icons\Word.png"


If Dir(sFile) = "" Then
MsgBox "Picture file was not found in path!", , "No such animal"
Exit Sub
End If
Set cl = Range(Selection.Address) 'set active cell
clLeft = cl.Left
clTop = cl.Top


ActiveSheet.Shapes.AddPicture Filename:=sFile, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoCTrue, _
    Left:=clLeft, Top:=clTop, _
    Width:=12, _
    Height:=12
    
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this. Add the code to right above End Sub.

Code:
Dim sh as Shape
For Each sh in ActiveSheet.Shapes
If sh.Top = clTop and sh.Left=clLeft Then
Exit For
End If
End For

ActiveSheet.Hyperlinks.Add Anchor:=sh, Address:=sText
 
Last edited:
Upvote 0
Thanks for your response!

I get a syntax error on the end for line when i paste this aboven my End Sub
 
Upvote 0
Here is a cleaned up version of your original code:

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Private Sub InsertWord_Click()

    Dim cl As Range, oPic As Shape
    Dim clipboardData As Object 'get the info from clipboard
    Dim clipboardContents As String 'get the info from clipboard
    Dim clLeft As Double 'select the active cell
    Dim clTop As Double 'select the active cell
    Dim sFile As String 'select the picture to be added
    Dim lCounter As Long

    Set clipboardData = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    clipboardData.GetFromClipboard

    For lCounter = 1 To UBound(Application.ClipboardFormats)
        If Application.ClipboardFormats(lCounter) = xlClipboardFormatText Then
            If IsValidURL(ByVal 0&, StrConv(clipboardData.GetText(lCounter), vbUnicode), 0) = 0 Then
                clipboardContents = clipboardData.GetText(lCounter)
                Exit For
            Else
                MsgBox "There is no valid URL in the clipboard.", vbCritical
                Exit Sub
            End If
         Else
            MsgBox "There is no valid URL in the clipboard.", vbCritical
            Exit Sub
        End If
    Next lCounter
         
    sFile = "C:\Insert Icons\Word.png"
    sFile = "C:\Users\Info-Hp\Pictures\bbb.png"
    
    If Dir(sFile) = "" Then
        MsgBox "Picture file was not found in path!", , "No such animal"
        Exit Sub
    End If
    
    Set cl = ActiveWindow.RangeSelection
    clLeft = cl.Left
    clTop = cl.Top

    Set oPic = ActiveSheet.Shapes.AddPicture(Filename:=sFile, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoCTrue, _
    Left:=clLeft, Top:=clTop, _
    Width:=12, _
    Height:=12)
        
    If Not oPic Is Nothing Then
        ActiveSheet.Hyperlinks.Add Anchor:=oPic, Address:=clipboardContents
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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