0

How can I capture an Event based on "an Image has been pasted to the current worksheet in whatever Target cell"?

  • I know how to detect a "Paste" action in a worksheet, as per this question here. However, this is based on Worksheet_Change which doesn't trigger when pasting an Image.
  • I also had a look at catching all paste events as explained in this article, but that simply checks for all options (keyboard, mouse, command bar). I would still have to manually do a check for "was it an image that was pasted".

What I am trying to figure out, is basically what happens in that first link (hook into Worksheet_Change event and proceed when the Change was based on a Paste), but can also detect the fact that a Shape object has been added to the worksheet, where Shape.Type is 13 (as per these enumerations)

Any help is much appreciated.

Sample code:

Private Sub Worksheet_Change(ByVal Target As Range) 'Doesn't trigger on Image Paste
Dim ws As Worksheet
Dim imageCnt As Integer
Dim sh As Shape
Dim UndoList As String

Set ws = ActiveSheet
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
imageCnt = 0

If Left(UndoList, 5) = "Paste" Then 'When paste.
    If Target.Address = "$C$8" Then 'Where user is instructed to paste.
        With ws 'Check if there's a new image:
            For Each sh In .Shapes
                If sh.Type = msoPicture Then
                    imageCnt = imageCnt + 1
                End If
            Next sh
            If imageCnt > oldImageCnt Then 'oldImageCnt publicly declared elsewhere
                Set sh = .Shapes(.Shapes.Count)
                'Do stuff with the pasted image here.
            End If
        End With
    End If
End If
End Sub

Unfortunately, I can't just event sink the Shapes collection or something similar. Any advise/help is much appreciated.

2
  • The only way that I see this working is to run a Do:DoEvents:Loop and keep checking the picture count.
    – TinMan
    Oct 15, 2018 at 15:55
  • Hm, that would be a possible work around. I'd still prefer capturing "all pastes" and then checking whether I have increased picture count to minimize background code. Oct 15, 2018 at 16:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.