There is a task I must perform daily. It involves copying information from excel into company software. I don't have any control over this particular piece of company software. I am trying to automate the task. It involves copying selected cells into the clipboard and then making 9 mouse clicks through the company software, pausing about half a second after each click.
It all seems to work, except the last step. I get a message that there is nothing in the clipboard. But as soon as the macro exits, the information is in the clipboard. I paused the macro for 30 seconds just to make sure I wasn't rushing the clipboard. It still didn't recognize the clipboard as full until right after the macro had completed.
Its as if range.copy copies to the clipboard but does not commit changes until after the macro has completed. They are not finalized until the macros are completed.
I don't know if I am missing some command like : clipboard.acceptchanges or clipboard.finalize or clipboard.refresh. I am not sure what I am missing. It seems that other people have similar problems caused by time delays of the clipboard, but as I said it isn't simply a time delay issue for me. It happens no matter how much time I give it.
Thanks.
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
Public Declare PtrSafe Function CountClipboardFormats Lib "user32" () As Long
Worksheets("writeorder").Range("M" & firstrow & ":M" & lastrow).Copy
Sleep 500
Call transactionclicksworkcomp
Sub transactionclicksworkcomp()
SetCursorPos 517, 1059 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 500
SetCursorPos 954, 33 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 500
SetCursorPos 659, 1029 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 500
SetCursorPos 588, 898 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 1200
SetCursorPos 767, 895 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 1200
SetCursorPos 705, 718 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 1200
SetCursorPos 1668, 889 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 1200
SetCursorPos 1852, 897 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 1200
SetCursorPos 1852, 885 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Sleep 3000
End Sub
The last click is on a paste button within my work application that retrieves info from the clipboard. The macro clicks the paste and the software shoots me an error message(empty clipboard). But when I click the button directly after the macro is finished, the information is pasted correctly.