Embed a Picture (with Code)

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,051
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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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