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.
printmediaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
The anchor for the hyperlink should be the shape itself, not it's name.
ActiveSheet.Hyperlinks.Add Anchor:=shp, Address:=rsPubs3.Fields("OEM_ID_WebsitePath ").Value

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
printmediaAuthor Commented:
That was it, I had to use shp instead of shp.Name

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.