Embed a Picture (with Code)

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,098
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I have a .bmp image I'd like on my worksheet via code, but not having much success.

Code:
pname= "D:\logo4.bmp"
With Worksheets("Test")
  .shapes.addpicture(pname,False, True,100, 10, -1, -1)
End With

It won't compile, tells me "Expected = "

Thanks for any help
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try removing the brackets around the arguments for AddPicture...

Code:
.Shapes.AddPicture pname, False, True, 100, 10, -1, -1

Hope this helps!
 
Upvote 0
Something like this. This is some old code, so you can adjust the parameters of the addpicture function.

Code:
Private Sub InsertPic(filePath As String, ByVal insertCell As Range)
    Dim xlShapes As Shapes
    Dim xlPic As Shape
    Dim xlWorksheet As Worksheet

    If IsEmpty(filePath) Or Len(Dir(filePath)) = 0 Then
        MsgBox ("File Path invalid")
        Exit Sub
    End If

    Set xlWorksheet = Sheets("Test")
    
    Set xlPic = xlWorksheet.Shapes.AddPicture(filePath, msoFalse, msoCTrue, insertCell.Left, insertCell.Top, insertCell.Width, insertCell.Height)
    xlPic.Placement = xlMoveAndSize
    xlPic.LockAspectRatio = msoCTrue
End Sub

Sub test()
InsertPic "C:\Users\Pictures\Picture.bmp", Range("A1")
End Sub
 
Upvote 0
Many thanks, it was the brackets, but much appreciate the other example.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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