[英]assign macro to image in excel
I have inserted an image with the following codes.我已插入带有以下代码的图像。 Next, I want to select the newly inserted image immediately and assign a macro to picture and apply some formatting with it.接下来,我想立即对新插入的图像 select 并为图片分配一个宏并对其应用一些格式。 I need your help.我需要你的帮助。
Sub InsertPicUsingShapeAddPictureFunction()
Dim profile As String
On Error GoTo 0
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Picture Files", "*.bmp;*.jpg;*.gif;*.png"
.ButtonName = "Select"
.AllowMultiSelect = False
.Title = "Choose Photo"
.InitialView = msoFileDialogViewDetails
.Show
End With
ActiveSheet.Range("B3").Select
ActiveSheet.Shapes.AddPicture Filename:=fd.SelectedItems(1), _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoCTrue, _
Left:=ActiveSheet.Range("B3").Left + 2, _
Top:=ActiveSheet.Range("B3").Top + 2, _
Width:=123, _
Height:=134
'here I am trying to select the inserted picture but the error is displayed
ActiveSheet.Shapes(fd.SelectedItems(1)).select
With selection
.onaction = "CustomMacro"
.placement = 1
End with
End Sub
If you assign a variable to the shape it is easy to reference it afterwards如果将变量分配给形状,则以后很容易引用它
Dim myShape as shape
set myShape = ActiveSheet.Shapes.AddPicture (Filename:=fd.SelectedItems(1), _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoCTrue, _
Left:=ActiveSheet.Range("B3").Left + 2, _
Top:=ActiveSheet.Range("B3").Top + 2, _
Width:=123, _
Height:=134)
You can then use the shape like this然后你可以使用这样的形状
myShape.TypeHereYourProperty ...
When you add a Shape, it will be the last Shape on the sheet, so:添加 Shape 时,它将是工作表上的最后一个 Shape,因此:
Sub InsertPicUsingShapeAddPictureFunction()
Dim profile As String, sh As Shape
On Error GoTo 0
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Picture Files", "*.bmp;*.jpg;*.gif;*.png"
.ButtonName = "Select"
.AllowMultiSelect = False
.Title = "Choose Photo"
.InitialView = msoFileDialogViewDetails
.Show
End With
ActiveSheet.Range("B3").Select
ActiveSheet.Shapes.AddPicture Filename:=fd.SelectedItems(1), _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoCTrue, _
Left:=ActiveSheet.Range("B3").Left + 2, _
Top:=ActiveSheet.Range("B3").Top + 2, _
Width:=123, _
Height:=134
'here I am trying to select the inserted picture but the error is displayed
Set sh = ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
With sh
.OnAction = "CustomMacro"
.Placement = 1
End With
End Sub
(Whenever I put a Shape on a sheet, I give it a Name
so I can refer to it later) (每当我在一张纸上放置一个形状时,我都会给它一个Name
,以便我以后可以参考它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.