Sign in to follow this  
Followers 0
Akshay07

Question about the use of Clipboard function

3 posts in this topic

#1 ·  Posted (edited)

Hello all,

I have a question about the use of the Clipboard function.

I see a problem in my script, but it is very random and very difficult to reproduce, therefore I would like some help on the code (but I am unable to retest it in a few minutes, it will take many attempts). if you see any mistake in the code, could you please let me know?

Here is what the script does:

1/ Take a screen capture of a window and paste it into an excel spreadsheet:

$hBitmap = _ScreenCapture_CaptureWnd("", $hWnd)
_ClipBoard_Open($hWnd)
_ClipBoard_Empty()
_ClipBoard_SetDataEx($hBitmap, $CF_BITMAP)
_ClipBoard_Close()
$oExcel.Application.ActiveSheet.Range($sSTCell).Select
$oExcel.Application.ActiveSheet.Paste
_WinAPI_DeleteObject($hBitmap)

2/ Copy/paste some text from another document into the excel spreadsheet using "CTRL+c" and "CTRL+v" (this is achieved using the Send function)

This works almost all the times, but some times, when I send "CTRL+v", the screen capture is pasted again instead of the text selected in 2/

I was thinking of changing 1/ into this:

$hBitmap = _ScreenCapture_CaptureWnd("", $hWnd)
    _ClipBoard_Open($hWnd)
    _ClipBoard_Empty()
    _ClipBoard_SetDataEx($hBitmap, $CF_BITMAP)
    $oExcel.Application.ActiveSheet.Range($sSTCell).Select
    $oExcel.Application.ActiveSheet.Paste                                              ; Paste image
    _ClipBoard_Empty()
    _ClipBoard_Close()
    _WinAPI_DeleteObject($hBitmap)

Could this help?

Thanks!

A.

Edited by Akshay07

Share this post


Link to post
Share on other sites



Maybe. But it's more likely that the result of clearing the clipboard will result in nothing being available to paste in step 2. My guess is that it's the Ctrl+C that's getting dropped...maybe your script isn't waiting for the "other" window to become fully active before copying?

I'd consider checking the contents of the clipboard after you send the Ctrl+C...if it's a bitmap, re-activate the "other" window, wait 100ms or so and re-send a Ctrl+C. Maybe loop that over and over until the contents of the clipboard are a text string before continuing on with the Ctrl+V part of your script.


"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Maybe. But it's more likely that the result of clearing the clipboard will result in nothing being available to paste in step 2.

Thanks for your answer. But at this stage, I still did not copy anything related to step 2.

I just don't understand why it works only 99% of the time

I'd consider checking the contents of the clipboard after you send the Ctrl+C...if it's a bitmap, re-activate the "other" window, wait 100ms or so and re-send a Ctrl+C. Maybe loop that over and over until the contents of the clipboard are a text string before continuing on with the Ctrl+V part of your script.

Sounds good! I'll try that.

Edited by Akshay07

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0