I am trying to extract content from a large library of Word documents using a VBA project in Excel. The machine is Windows 10 64 bit.
I need to use EmptyClipboard
but I can't get it to do anything - no error, but the clipboard content is unchanged afterwards (I am looking at both the Office clipboard and Windows clipboard and they are both unaffected).
Here is the code I am using:
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr
Function ClearClipboard()
Dim ptr As LongPtr
On Error GoTo failed
ptr = OpenClipboard(0&)
MsgBox "Open " & ptr
ptr = -1
ptr = EmptyClipboard()
MsgBox "Empty " & ptr
CloseClipboard
Exit Function
failed:
Debug.Print Err.Description
End Function
EDIT
@drake-wu-msft, I will check out that link - thanks.
In the meantime, further testing following up question from @Rory just makes it weirder. I used this process:
Clear Windows clipboard (using Windows key + V, clear all) Clear Office clipboard (Using Clear All option on clipboard viewer)
Both clipboards appear empty and Ctrl+V pastes nothing
Run this (fragment) ...
wordApp.Documents.Open (filePath)
wordApp.Documents(filePath).Activate 'Breakpoint A
Call ClearClipboard
wordApp.Documents(filePath).Select 'Breakpoint B
ActiveDocument.Range.CopyAsPicture '
imgPath = savePic(ID) 'Breakpoint C
...
Function ClearClipboard()
On Error GoTo failed
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
Exit Function
failed:
Debug.Print Err.Description
End Function
Breakpoint results: A - both clipboards empty B - both clipboards empty C - both clipboards contain data from before the initial (manual) clearing - not what was just copied by the CopyAsPicture command
Ok - thanks all for your time and answers - confession of stupidity time:
All I had to do was to turn off the Windows clipboard history. If this is off the clipboard only retains the last item copied which is what I need. (It also means that you can't see the clipboard contents using Windows+V, which was why I had turned it on for testing.)
So EmptyClipboard only clears the last item if history is on (which is of course is the whole clipboard if history is off).
If history is on then you'll have to use ClearHistory via the dll stuff above - I dodn't get that far before the lightbulb went on.