To name a picture as you paste it from VBA, you can use the following steps:
- Copy the picture to the clipboard using the following code:
Formula:
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
- Create a new shape in Excel using the following code:
Formula:
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 100, 100, 200, 200).Select
- Paste the picture from the clipboard into the new shape using the following code:
Formula:
ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
ActiveSheet.Paste
- Rename the shape to "samplepicture" using the following code:
Formula:
ActiveSheet.Shapes("Rectangle 1").Name = "samplepicture"
So, the complete code to paste the picture from the clipboard and name it as "samplepicture" would be:
Formula:
Sub PasteAndNamePicture()
'Copy the picture to the clipboard
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Create a new shape in Excel
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 100, 100, 200, 200).Select
'Paste the picture from the clipboard into the new shape
ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
ActiveSheet.Paste
'Rename the shape to "samplepicture"
ActiveSheet.Shapes("Rectangle 1").Name = "samplepicture"
End Sub