Serdar
Serdar

Reputation: 1516

How can I use Shapes.AddShape method to add Camera Object

If I record the macro. I see this code.

Selection.Copy
ActiveSheet.Shapes.AddShape(, 480.75, 171#, 63#, 63#).Select
ActiveSheet.Shapes.Range(Array("Picture 2")).Select
Application.CutCopyMode = False

When I looked at the MsoAutoShapeType enumaration I couldn't see camera object.

And if I change any value it gives exception... (, 480.75, 171#, 63#, 63#) the documentation says that these values are top,left,width and height ...

I want to write a method that can create camera objects any range I give

Sub TakePhoto(myRange As String, myPicture As String)

How can I achieve this?

Upvotes: 1

Views: 1693

Answers (1)

Doug Glancy
Doug Glancy

Reputation: 27488

Try this:

Sub TakePhoto(rngSource As Excel.Range, rngTarget As Excel.Range)
Dim ws As Excel.Worksheet
Dim shpPicture As Excel.Shape

Set ws = rngTarget.Parent
rngSource.Copy
ws.Pictures.Paste Link:=True
Set shpPicture = ws.Shapes(ws.Shapes.Count)
With shpPicture
    .Top = rngTarget.Top
    .Left = rngTarget.Left
End With
End Sub

Call it like this:

Sub test()
TakePhoto Sheet2.Range("A1:C4"), Sheet1.Range("c5")
End Sub

Upvotes: 1

Related Questions