Inserting Image into cell with VBA

overKBV

New Member
Joined
Sep 15, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
So I am using this to add pictures to my file. I am essentially creating an asset list and want to put the picture with the asset on that line. It essentially then creates a link to open the photo in its original size. The problem is it works sometimes. I suspect it might have to do with the size/resolution of the photo. Can I force Excel to accept larger photos or, using VBA, compress the photo? I have tried bulk compressing my photos but it has not been easy.


VBA Code:
Sub AddPictureNotInsert()
    Dim strFileName As String
    Dim objPic As Shape
    Dim rngDest As Range
    strFileName = Application.GetOpenFilename( _
        FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
    If strFileName = "False" Then Exit Sub
    Set rngDest = ActiveCell
    Set objPic = ActiveSheet.Shapes.AddPicture(strFileName, False, True, 10, 10, -1, -1)
    With objPic
        .LockAspectRatio = msoFalse
        .Left = rngDest.Left + 1
        .Top = rngDest.Top + 1
        .Width = rngDest.Width - 2
        .Height = rngDest.Height - 2
    End With
    ActiveSheet.Hyperlinks.Add Anchor:=objPic, Address:=strFileName
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
2,259
Office Version
  1. 2016
Platform
  1. Windows
Maybe you can make this part of your code.

 

Forum statistics

Threads
1,186,178
Messages
5,956,394
Members
438,249
Latest member
georgebasalic3

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
Top