Add Image Button

OKCIrish

New Member
Joined
Dec 3, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello,

I have this VBA code, attached below, to react to a form control of a button. Anytime the user clicks the button it will pull up their photos and they can select the photo they want to use and it will paste it in a certain cell/section. After the report is done, everything gets converted to PDF and the pictures are there on the PDF report. However, my issue is that if I need to fix something on their report or they have to send the excel report to me, the link for the pictures becomes broken and the pictures won't send with the file or display correctly on the excel file I receive. Can I get some help on this coding to make it where the picture is attached to the file instead of it being pasted? I have attached an image since is a form control and won't show anything if I do XL2BB.

Image Tab.PNG


VBA Code:
Sub AddImages1()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range

strFileName = Application.GetOpenFilename( _
    FileFilter:="Images,*.jpg;*.gif;*.png", _
    Title:="Please select an image...")

If strFileName = "False" Then Exit Sub

Set rngDest = Worksheets("Images").Range("B12:F12")
Set objPic = Worksheets("Images").Pictures.Insert(strFileName)

With objPic

  .Height = rngDest.Height
  .Width = rngDest.Width
  .Left = rngDest.Left
  .Top = rngDest.Top

End With

Set rngDest = Nothing
Set objPic = Nothing

End Sub
 

Attachments

  • Image Tab.PNG
    Image Tab.PNG
    23.3 KB · Views: 21

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try using the AddPicture method instead, like this...

VBA Code:
Sub AddImages1()

    Dim strFileName As String
    Dim objPic As Shape
    Dim rngDest As Range
    
    strFileName = Application.GetOpenFilename( _
        FileFilter:="Images,*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
    
    If strFileName = "False" Then Exit Sub
    
    Set rngDest = Worksheets("Images").Range("B12:F12")
    
    With rngDest
        Set objPic = Worksheets("Images").Shapes.AddPicture(Filename:=strFileName, linktofile:=msoFalse, savewithdocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    End With
    
    Set rngDest = Nothing
    Set objPic = Nothing

End Sub

Notice that objPic is now declared as Shape.

Does this help?
 
Upvote 0
Solution
Try using the AddPicture method instead, like this...

VBA Code:
Sub AddImages1()

    Dim strFileName As String
    Dim objPic As Shape
    Dim rngDest As Range
   
    strFileName = Application.GetOpenFilename( _
        FileFilter:="Images,*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
   
    If strFileName = "False" Then Exit Sub
   
    Set rngDest = Worksheets("Images").Range("B12:F12")
   
    With rngDest
        Set objPic = Worksheets("Images").Shapes.AddPicture(Filename:=strFileName, linktofile:=msoFalse, savewithdocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    End With
   
    Set rngDest = Nothing
    Set objPic = Nothing

End Sub

Notice that objPic is now declared as Shape.

Does this help?
It does help and make the picture stay when sending as an excel file, however the picture doesn't auto fit to its area like the old code used to. Any help with that? I have attached comparison of your code provided to the old one.
 

Attachments

  • Image Tab.PNG
    Image Tab.PNG
    63.4 KB · Views: 16
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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