Inserting Photos Via Macro

overKBV

New Member
Joined
Sep 15, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have the code below. It works fine with photos taken in landscape but not with photos in portrait. How can I adjust so it just takes whatever photo? It doesn't really matter what the photo looks like in the spreadsheet just so you can see it, click on it, and open the original.
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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your code sizes the photo to the destination cell. Small cell = small photo.

Start by changing the LockAspectRatio property to true.

VBA Code:
.LockAspectRatio = msoTrue
 
Upvote 0
Okay, I tried that it did not make a difference. I also tried making the cell larger and that did not work. Is there a way to force the photo to be the same size without cropping it?
 
Upvote 0
Okay, I tried that it did not make a difference. I also tried making the cell larger and that did not work. Is there a way to force the photo to be the same size without cropping it?

That is odd, because it It makes a big difference (an enormous difference, in fact) when I run the exact code you posted. Perhaps you can describe the issue in more detailed terms. What happens when you use the F8 key in the VBE debugger to single step though your code? At which step does the image change to something you do not like?
 
Upvote 0
I did not describe the problem completely. Landscape photos are added to the cell fine, portrait photos do not show up at all. Like the cell remains empty. It's as if the photo is too large and just is not added. The code seemingly works fine for the landscape photos. Just not at all for the portrait photos.
 
Upvote 0
I tested your code on both landscape and portrait. No issues on either except the image was smashed into the size & shape of the activecell - but that is how you coded it. I still think you need to do the F8 single step test on both a portrait and landscape example.
 
Upvote 0
I tested your code on both landscape and portrait. No issues on either except the image was smashed into the size & shape of the activecell - but that is how you coded it. I still think you need to do the F8 single step test on both a portrait and landscape example.
So, I have deduced that it is not a portrait versus landscape problem. It must be a source problem that will take a little more investigation. Thanks for your time, I will get back to you if I figure out the problem
 
Upvote 0

Forum statistics

Threads
1,224,974
Messages
6,182,106
Members
453,088
Latest member
Chaoxite

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