VBA code to insert a picture as shape background.

amaresh achar

Board Regular
Joined
Dec 9, 2016
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a rectangle shape say 5cm x 8cm in an excel sheet. My requirements are :

1. When I click on that rectangle shape, it should prompt me to select a picture.

2. After I select that picture, it should set that picture as the background of that rectangle shape.

3. Compress that picture to e-mail (96ppi) levels

Thanks in advance for the assistance.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,
1. Insert the code below in a standard code module:
VBA Code:
Sub setBackGround()
    Dim shName As String
    shName = Application.Caller
    Dim x As FileDialog
    Set x = BrowseForFile
    If x Is Nothing Then Exit Sub
    Dim fName As String
    fName = x.SelectedItems(1)
    Dim shp As Shape
    Set shp = ActiveSheet.Shapes(shName)
    With shp.Fill
        .Visible = msoTrue
        .UserPicture fName
        .TextureTile = msoFalse
        .RotateWithObject = msoTrue
    End With
    DoEvents
    shp.Select
    SendKeys "%e", True
    SendKeys "~", True
    Application.CommandBars.ExecuteMso "PicturesCompress"
    ActiveCell.Select
    Set x = Nothing
    Set shp = Nothing
End Sub

Function BrowseForFile() As FileDialog
    Set BrowseForFile = Application.FileDialog(msoFileDialogFilePicker)
    With BrowseForFile
        .Title = "Select files"
        .AllowMultiSelect = False
        .Filters.Add "JPG files", "*.jp*g", 1
        .Filters.Add "All files", "*.*", 2
        If .Show = 0 Then Set BrowseForFile = Nothing
    End With
End Function
2. right-click on the shape and select Assign Macro ...
3. select setBackGround
you're all set

Points Of Attention:
1. I personally don't like the SendKeys method. For starters it messes with my Num-Lock status.
2. I would suggest compressing pictures before inserting - plenty of batch tools are available for this. Check out this thread also:
Re-Save and compress images from a folder
2. This code is put together quickly and is not extensively tested.
 
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,416
Members
449,099
Latest member
COOT

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