VBA - 双击插入的图片时如何运行起作用
VBA - How to run function when double click an inserted image
双击插入的图片如何触发excelvba功能?现在该功能由 Private Sub Workbook_AfterSave(ByVal Success As Boolean)
.
触发
我只能找到单击单元格或单击超链接时的示例,但找不到双击图像的示例。
Excel 图像没有双击事件(如 Access)。工作表有 Worksheet_BeforeDoubleClick
:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox ("Application.Caller: " & IIf(IsError(Application.Caller), Err.Description, _
Application.Caller) & vbLf & "Target.Address: " & _
IIf(IsError(Target.Address), Err.Description, Target.Address))
End Sub
我不确定如何检查它是否是被双击的图像 -- Application.Caller returns 如果它不是被选中的单元格,则会出错。
您可以将图像放在单元格后面,作为背景图像,然后双击其上方的单元格进行操作。
.
或者,我想你可以让图像的指定宏检查自上次单击它以来已经过了多长时间。如果图像在 400 毫秒内被单击两次,此示例将显示 MsgBox
:
Public clickedPic As String
Public lastTimer As Single
Sub Picture2_Click()
clickedPic = Application.Caller
If (Timer - lastTimer) < 0.5 Then
MsgBox "doubleclick"
er
End If
lastTimer = Timer
Debug.Print clickedPic, Now()
End Sub
您可以将 OnAction 属性 分配给单击事件后激活程序的形状。
添加了矩形的代码示例
当然,您可以针对现有形状轻松修改它。
Sub AddRectangle()
Dim oSht As Worksheet
Dim oShape As Shape
Set oSht = ThisWorkbook.Worksheets("MySheet")
' only if you want to add a new shape, otherwise refer to Name or item no
Set oShape = oSht.Shapes.AddShape(msoShapeRectangle, 100, 100, 50, 50)
oShape.Name = "MyRectangle"
' OnAction property assignes the specified procedure (e.g. "MyProcedure") to the shape object.
oSht.Shapes("MyRectangle").OnAction = "MyProcedure"
End Sub
Sub MyProcedure()
MsgBox "Shape MyRectangle has been clicked."
End Sub
双击插入的图片如何触发excelvba功能?现在该功能由 Private Sub Workbook_AfterSave(ByVal Success As Boolean)
.
我只能找到单击单元格或单击超链接时的示例,但找不到双击图像的示例。
Excel 图像没有双击事件(如 Access)。工作表有 Worksheet_BeforeDoubleClick
:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox ("Application.Caller: " & IIf(IsError(Application.Caller), Err.Description, _
Application.Caller) & vbLf & "Target.Address: " & _
IIf(IsError(Target.Address), Err.Description, Target.Address))
End Sub
我不确定如何检查它是否是被双击的图像 -- Application.Caller returns 如果它不是被选中的单元格,则会出错。
您可以将图像放在单元格后面,作为背景图像,然后双击其上方的单元格进行操作。
.
或者,我想你可以让图像的指定宏检查自上次单击它以来已经过了多长时间。如果图像在 400 毫秒内被单击两次,此示例将显示 MsgBox
:
Public clickedPic As String
Public lastTimer As Single
Sub Picture2_Click()
clickedPic = Application.Caller
If (Timer - lastTimer) < 0.5 Then
MsgBox "doubleclick"
er
End If
lastTimer = Timer
Debug.Print clickedPic, Now()
End Sub
您可以将 OnAction 属性 分配给单击事件后激活程序的形状。
添加了矩形的代码示例
当然,您可以针对现有形状轻松修改它。
Sub AddRectangle()
Dim oSht As Worksheet
Dim oShape As Shape
Set oSht = ThisWorkbook.Worksheets("MySheet")
' only if you want to add a new shape, otherwise refer to Name or item no
Set oShape = oSht.Shapes.AddShape(msoShapeRectangle, 100, 100, 50, 50)
oShape.Name = "MyRectangle"
' OnAction property assignes the specified procedure (e.g. "MyProcedure") to the shape object.
oSht.Shapes("MyRectangle").OnAction = "MyProcedure"
End Sub
Sub MyProcedure()
MsgBox "Shape MyRectangle has been clicked."
End Sub