VBA – 如何双击插入的图像时运行function

当我双击插入的图像时如何触发excel vba函数? 现在这个函数被Private Sub Workbook_AfterSave(ByVal Success As Boolean)触发。

我只能find单击单元格或单击超链接时的示例,但无法find一个双击图像。

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不是被选中的单元格,它将返回一个错误。

您可以将图像放在细胞后面 ,作为背景图像,然后双击上方的细胞。

或者,我想你可以让图像分配的macros检查自从上次被点击以来已经有多长时间了。 如果图像在400ms内被点击两次,这个例子将显示一个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 (eg "MyProcedure") to the shape object. oSht.Shapes("MyRectangle").OnAction = "MyProcedure" End Sub Sub MyProcedure() MsgBox "Shape MyRectangle has been clicked." End Sub 
Interesting Posts