Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Win32::Clipboard dying?

by manish.rana@me.com (Initiate)
on May 19, 2016 at 05:48 UTC ( [id://1163415]=perlquestion: print w/replies, xml ) Need Help??

manish.rana@me.com has asked for the wisdom of the Perl Monks concerning the following question:

$clip = Win32::Clipboard(); sub select_and_paste_into_ppt() { $clip->Empty(); $sheet_to_rename->UsedRange->Select; $sheet_to_rename->UsedRange->Copy; save_image(); paste_image_to_ppt(); $Title->TextFrame->TextRange->{Text} = $sheet_to_rename->{Name} +; $image_count++; add_new_slide(); $clip->Empty(); } sub save_image() { $image = Win32::Clipboard::GetBitmap() or die "\nNo Image Captured + \n"; open BITMAP, ">$path.\\Images\\$image_count.jpg"; binmode BITMAP; print BITMAP $image; close BITMAP; }
Hi Everyone, Above is my perl code to select the cells in excel's active sheet and put the image file in a particular folder. This routine works fine. But if this runs continously for more than 80+ times I get an error "\nNo Image Captured \n" and the program dies(I can remove the die command and let the program continue without any problem but it will skip that particular image). Restarting the computer solves this problem. I have to run this script on the machines that I can not restart. I guess I am opening too many clipboards somehow? Can anyone point me to the right direction?

Replies are listed 'Best First'.
Re: Win32::Clipboard dying?
by BrowserUk (Patriarch) on May 19, 2016 at 06:06 UTC

    Have you tried calling GetBitmap() as a instance method rather than a class method:$image = $clip->GetBitmap() or die .. ;

    Have you tried: $clip->WaitForChange(); prior to GetBitmap()?

    Have you tried undefing and re-creating the clipboard object when a failure occurs?

    RETRY: my $tries = 0; my $image = $clip->GetBitmap() or do { undef $clip; $clip = Win32::Clipboard->new(); goto RETRY unless ++$retries > 5; } ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      HI BrowserUK, Thank you for your reply. No I have not tried anything that you asked me. I am not seeing this problem after restarting my computer. I will try your code once I see this problem again. I did try to close the instance with something like $clip->Close(); But there was no such command. I did not know about undef $clip till now. Thank you.
      HI, Can you also tell me how can I get an email notification if anyone has replied to my question? I can not find that feature on this site somehow. Thank you.
        Can you also tell me how can I get an email notification if anyone has replied to my question?

        You can't.

        However, if you go to your user setting and check the box next to "/msg me when there's a reply to one of my posts", you will only need to log in to be notified of all the replies to any of your posts. (See the right hand side of the screen under the heading "chatterbox".)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32::Clipboard dying?
by afoken (Chancellor) on May 19, 2016 at 06:00 UTC
    ~~~A lot of LINE NOISE omitted. ~~~ Can anyone point me to the right direction?

    Start by editing your post. Add <code>...</code> around the code. You did notice that you posting was hardly readable when you hit the preview button, didn't you?

    Then check if and how Win32::Clipboard reports errors. My guess is $!, $@, or $^E. Add that to the message passed to die.

    Update: A quick look at Clipboard.xs and Clipboard.pm shows that there is no explicit error handling, so your best bet may be $^E, which hides a call to GetLastError().

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Hi afoken, Sorry, that was my first time using the site and I am fairly new at perl. I have edited the post as per your suggestions. The error message is simple the message that I have written after .... or die "No Image Captured".
        The error message is simple the message that I have written after .... or die "No Image Captured".

        Read my posting again. I told you where perl hides more information, and that you should write that information somewhere when an error occurs.

        Compare to the usual idiom for opening files:

        open my $f,'<',$filename or die "Can't open $filename: $!"; # ***HINT*** ------------------------------------------^^

        Also note that $! will most likely not help you, see my first posting.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1163415]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-18 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found