0

I have an windows form that adds images into the clipboard for processing purposes then when i try to clear the clipboard i still see the images in my clipboard if i go to excel. Why is it not clearing?

Dim xlsApp1 As Object
Dim xlsWB As Object
Dim xlsSheet As Object
xlsApp1 = CreateObject("Excel.Application")
xlsWB = xlsApp1.Workbooks
xlsApp1.DisplayAlerts = False
xlsWB.Open("myexcel")
xlsSheet = xlsApp1.ActiveSheet

For Each xlsShape As Excel.Shape In xlsSheet.Shapes
    xlsShape.Copy()
    If xlsShape.Name = "Picture 1" Then
        'process the image
    End If
next

xlsWB.Close()
xlsApp1.Quit()

    Try
        Marshal.ReleaseComObject(xlsSheet)
        Marshal.ReleaseComObject(xlsWB)
        Marshal.ReleaseComObject(xlsApp1)
        xlsSheet = Nothing
        xlsWB = Nothing
        xlsApp1 = Nothing
    Catch ex As Exception
        xlsSheet = Nothing
        xlsWB = Nothing
        xlsApp1 = Nothing
    Finally
        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try

Clipboard.Clear()
Clipboard.SetDataObject(0)
'but still see it in the clipboard.

 'close excel ect.....
  • I did a Clipboard.Set followed by Clipboard.Clear and it was cleared. – dbasnett Mar 08 '19 at 15:46
  • @dbasnett I do an shape copy which puts the image in the clipboard, then when i try to clear, its still there. – AnotherPerson Mar 08 '19 at 15:57
  • Can you please share `how you are putting the image in the clipboard`? The handles to the image might not be getting freed... – Trevor Mar 08 '19 at 15:58
  • @Çöđěxěŕ take a look at edit post. – AnotherPerson Mar 08 '19 at 16:03
  • Well my assumptions are correct, the handles are not being freed because they are tied up in the Excel instances... You would need to dispose the objects that have locks on them in order for them to be released from the clipboard... – Trevor Mar 08 '19 at 16:05
  • @Çöđěxěŕ how do i freed the handles or handle this situation? I dispose and releases excel as needed ect. – AnotherPerson Mar 08 '19 at 16:06
  • You need to release all of the `Excel` objects you created first before trying to clear them from the clipboard... Please [see](https://stackoverflow.com/a/15698337/1797425) this answer to help you close and dispose of your `Excel` objects. – Trevor Mar 08 '19 at 16:10
  • @Çöđěxěŕ take a look at my post, i do release my objects ect then tried to clear and it's still there. – AnotherPerson Mar 08 '19 at 16:12
  • Ok, thanks for updating your post, but please do this first so we are not guessing `what` you haven't done... Have you tried doing `Marshal.ReleaseComObject(xlsShape)` after the copy? – Trevor Mar 08 '19 at 16:14
  • if i release it after the copy, i can't use it to process ect, but i do still see it on my clipboard. – AnotherPerson Mar 08 '19 at 16:40
  • Clearing the clipboard is incorrect. An item should remain after it's used, until it is replaced by something else. – Joel Coehoorn Mar 08 '19 at 17:27
  • In for `For Each xlsShape` block right before the `Next` statement, add `xlsApp1 .CutCopyMode = False`. This should clear the object placed on the system clipboard by the `xlsShape.Copy()` statement. Note that this will not remove it from the _Office Clipboard_ that maintains a list of data objects placed on the system clipboard. – TnTinMn Mar 08 '19 at 18:11
  • I want to clear it because if the same excel gets opened again, its gonna want to copy the same images into the clipboard which I do not want to happen because that could lead to other issues. – AnotherPerson Mar 08 '19 at 18:22
  • @TnTinMn i need it to be removed from the office clipboard. – AnotherPerson Mar 08 '19 at 18:36
  • Office apps have their own clipboard implementation, extending the basic capabilities of the system clipboard. Most noticeable from theirs being able to store multiple items. So sure, clearing the system clipboard does also delete all of those items, that would defeat their usefulness. – Hans Passant Mar 08 '19 at 18:57

0 Answers0