Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Add Hyperlink to image added through Excel VBA

Hi all.

I have the following Excel VBA code that inserts an image to each row based on value of each in each cell in column A. I would like to add a hyperlink to each image based on another field in the SQL table.  Here's the code to insert the image:

Dim I3
    Dim rng3 As Range
    Set rng3 = Range("A3")
Dim strSQL4aa As String

Dim shp As Shape
 strSQL4aa = "SELECT OEM_ID_ImagePath FROM myTable WHERE OEM_ID = '" & rng3.Value & "'"

       .Open strSQL4aa
If Not rsPubs3.EOF Then
rsPubs3.MoveFirst
For I3 = 0 To rsPubs3.Fields.Count - 1

Set shp = ActiveSheet.Shapes.AddPicture(Filename:=rsPubs3.Fields("OEM_ID_ImagePath").Value, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoCTrue, _
    Left:=rng3.Offset(, I3 + 24).Left + 20, _
    Top:=rng3.Offset(, I3 + 24).Top + 5, _
    Width:=60, _
    Height:=77)

            Next I3
            
      End If
rsPubs3.Close

Open in new window


I need to use ActiveSheet.Hyperlinks.Add but I don't know how to get the OEM_ID_WebsitePath value from the strSQL4aa  above.

I've tried the following, but received a type mismatch error at the Hyperlink.Add line:

Dim shp As Shape
 strSQL4aa = "SELECT OEM_ID_ImagePath, OEM_ID_WebsitePath  FROM myTable WHERE OEM_ID = '" & rng3.Value & "'"

       .Open strSQL4aa
If Not rsPubs3.EOF Then
rsPubs3.MoveFirst
For I3 = 0 To rsPubs3.Fields.Count - 1

Set shp = ActiveSheet.Shapes.AddPicture(Filename:=rsPubs3.Fields("OEM_ID_ImagePath").Value, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoCTrue, _
    Left:=rng3.Offset(, I3 + 24).Left + 20, _
    Top:=rng3.Offset(, I3 + 24).Top + 5, _
    Width:=60, _
    Height:=77)

ActiveSheet.Hyperlinks.Add Anchor:=shp.Name, Address:=rsPubs3.Fields("OEM_ID_WebsitePath ").Value

            Next I3
            
      End If
rsPubs3.Close

Open in new window


Any help would be appreciated.
Thank you in advance.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of printmedia
printmedia

ASKER

That was it, I had to use shp instead of shp.Name

Thank you!