如何使用VBA在Excel中裁剪选定的图片

我想在excel中裁剪选定的图片,并试图使用此代码:

Sub CropPicture() Dim shpCrop As Shape Dim sngMemoLeft As Single Dim sngMemoTop As Single Set shpCrop = Selection With shpCrop sngMemoLeft = .Left sngMemoTop = .Top With .PictureFormat .CropLeft = 10 .CropTop = 10 .CropBottom = 10 .CropRight = 10 End With .Left = sngMemoLeft .Top = sngMemoTop End With End Sub 

但是,这给我一个types不匹配运行时错误。 问题是这部分代码:Set shp = Selection

我怎样才能解决这个问题?

尝试

 Set shpCrop = ActiveSheet.Shapes(Selection.Name) 

如果你尝试? typename(selection) 在“即时”窗格中? typename(selection) ,您将获得“图片”,但可以通过其名称属性获取形状。