+ Reply to Thread
Results 1 to 29 of 29

Protect clipboard data from other programs trying to use clipboard at same time?

  1. #1
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Protect clipboard data from other programs trying to use clipboard at same time?

    Hello. This is my first post here, so here goes.

    I am running an automated macro spreadsheet that every minute or so copies data using the clipboard and pastes it. If before it gets pasted, I use the clipboard to copy something in an other spreadsheet, or other software copies something using the clipboard at the same time, the clipboard contents can get corrupted.

    Is there any way to protect the clipboard contents from other programs using the same clipboard area at the same time?

    I am assuming there is only one clipboard per computer? What about isolating a program and its clipboard use to a separate core perhaps? (Maybe I am dreaming there...).

    I know there are many ways to copy and paste without using the clipboard, but that would make rewritting "a lot" of code. Clipboard protection for each Excel program running would be easier. If there is such a thing.

    Thanks. - Chuck
    Last edited by chuckchuckit; 09-19-2010 at 03:20 AM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    I know there are many ways to copy and paste without using the clipboard, but that would make rewritting "a lot" of code.
    Writing copy-code in VBA is more elaborate than:

    Please Login or Register  to view this content.
    Besides this 'non-copying' copy method is faster than using the clipboard.



  3. #3
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Thanks for the response, but not sure I understand the "with" code.

    Following code works without clipboard but I can not find a way to get it to work for "value"
    Please Login or Register  to view this content.
    Also this line of code (which also does not use clipboard) but it does not copy the whole range unless full range is on both sides. With the usual copy/paste one just pastes to the upper left cell and whole range should paste:

    Please Login or Register  to view this content.
    Last edited by teylyn; 09-18-2010 at 09:10 AM.

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Chuck, please put your code in code tags, even if it's just one line.

    Following code works without clipboard
    No, it does not. You copy and you paste. You're using the clipboard. To rewrite this without using the clipboard, you'd need something along the lines of

    Please Login or Register  to view this content.
    Encasing in a With statement is not really required.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    You overloooked the resize method: it means the code adapts the destination.Range to the to-be-copied range.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Thanks much for your responses.

    I'll try the 2 methods you both suggested. It looks like using the word "copy" anywhere then uses the clipboard. Also on using "resize" does both source and destination need known exact sizes?

    It is sounding like there is no way to protect the clipboard data, as it is open season for any application to use at any time? Am I assuming correctly on that?

  7. #7
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    snb, as often, your code is very cryptic and not suited to the VBA beginner, or even intermediate, for that matter. I'm happy for you to be able to churn out such code, but to be really helpful for the OP, a bit of explanation would be in order.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    @teylyn

    I fear you overlooked my post #5.
    But feel free to ask what you do not understand specifically.

    Besides, a PM seems to me to be the appropriate method to distribute personal comments. Personal comments in a thread do not contribute to the solution of the TS's question.
    And yes, that also applies to this post.
    Last edited by snb; 09-18-2010 at 08:29 AM.

  9. #9
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Quote Originally Posted by chuckchuckit View Post
    Thanks for the response, but not sure I understand the "with" code.
    In this forum we try to describe a suggestion in a way that the OP can understand.

  10. #10
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    chuck, please do not change posts that have been replied to. It breaks the flow and logic of the thread. I will restore the original of your post #3. Feel free to create a new post with requests for clarification.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    I'm with teylyn here.
    Besides, a PM seems to me to be the appropriate method to distribute personal comments. Personal comments in a thread do not contribute to the solution of the TS's question.
    Nobody is asking about your love life, just a bit more help in understanding your outstanding code.
    Just ask yourself this "How long did I take to be as clever as this?" Then think of others.

    Another good example here.Post #7
    http://www.excelforum.com/excel-prog...-and-name.html

  12. #12
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    I am not sure, but unfortunately it looks like there is no way to protect data copied into the clipboard, if an other program copies their data into the clipboard before you paste what you copied there.

    Unless someone knows of a way to stop other programs from corrupting the clipboard before you use it to paste, I likely will have to rewrite all my copy routines so the clipboard is not used during any copying.

    From reading the "very helpful posts here" this is the code I have come up with that will:

    Copy cell data between worksheets (even inactive sheets) "without using the clipboard". Uses row and column cells format.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    The above line of code used to copy data "without" using the clipboard:

    Please Login or Register  to view this content.

    has a "with" version that also seems to work the same which is:

    Please Login or Register  to view this content.

    Is one of these versions of code faster or more efficient than the other?

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    For that example, they are the same.

    The With statement establishes an object that can be referenced in subsequent statements with just the dot:

    Please Login or Register  to view this content.
    The advantages of using the With statement are that it:

    o makes clear that the same object is being reused
    o makes lines of code that use it shorter, improving clarity
    o saves typing (no small advantage for the typing-impaired)
    o runs faster
    Entia non sunt multiplicanda sine necessitate

  15. #15
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    That kind of looks like a structure from my 'C' programming days (I am new to programming again).

    Could you give an example on how you would then "referenced in subsequent statements with just the dot" from your with example?

    Thanks.

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    There's three examples in my last post.

  17. #17
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    I am assuming that you declared a new "myRange" having those following properties?

  18. #18
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    So to change Bob's name to Ted, a next statement could be:

    Please Login or Register  to view this content.
    Is that correct?

  19. #19
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Yes, except within the scope of the With statement, you don't need to repeat myRange. That's the point.

  20. #20
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    So inside the same function I could just change Bob's name to Ted by later using a single line of code:

    Please Login or Register  to view this content.

    In same function, if I have 2 different "With" statements that both have a property of .value, would I then have to use the myRange to clarify which With I would be referring to for .value?

  21. #21
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Any given line of code is only within the scope of the most recent With ststement.
    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    I can see how the nested With's work. Very useful tight code.

    I am assuming that the .copy would be using the clipboard.

  23. #23
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Yes .

  24. #24
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Just a note of caution when you are using the With ... End With construct. If, for example, you are using it with ranges within sheets, this:

    Please Login or Register  to view this content.

    Is not the same as:

    Please Login or Register  to view this content.

    although, if Sheet1 is the active sheet, the result will be the same.

    If, however, Sheet1 is not the Active Sheet, the result will be unpredicatable.

    It can be a pig to debug because you tend to look at the sheet where the data is coming from, thus making it the Active Sheet. So, when you step through, it works; when you run it normally, it can fail.

    I may be particularly stupid, but I've spent many unhappy hours trying to work through issues that are caused by a missing dot!

    That's not to negate the benefits ... just be careful out there.

    Regards

  25. #25
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Just a little 'ole dot. I was just reading that using "Worksheets" will make inactive sheets valid. Such as using your example:

    Please Login or Register  to view this content.
    changed to:

    Please Login or Register  to view this content.
    is supposed to make inactive sheets valid. Perhaps a missing dot might still be trouble there too?

    I am still working on trying to make the .copy not use the clipboard. I am nesting an other with being set to the 2nd col but not quite there yet.

  26. #26
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    will make inactive sheets valid.
    Pardon?

    The reason to use Worksheets instead of Sheets is that the Sheets collection includes worksheets, chart sheets, macro sheets, and some other kind of sheet that I forget.

    Better yet is to use an object variable so you get IntelliSense:

    Please Login or Register  to view this content.
    As soon as you type the dot, you see all the properties and methods of a worksheet.

  27. #27
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Lot of info there for me to study. Due to time pressures I think I will for now use code without the nested "With's" as my understanding not quite all there yet.

    Following code should work to copy "without" using the clipboard:

    Please Login or Register  to view this content.

    It copies the values ok, but how would I get it to also copy the cell colors too?

    Thanks.

  28. #28
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    It looks like the CTRL-C and CTL-V copy and paste which uses the clipboard moves most everything. But it of course uses the clipboard which causes my programs running at same time to interfere with each other when clipboard gets corrupted when simultaneous use happens.

    Using the very helpful examples and suggestions here, I ended up learning that the other ways NOT using the clipboard takes a bit more coding to transfer other things besides just the value over. I preformatted the target cells for colors and such before I could then just transfer the values.

    It works great and I tried to crash the programs using the clipboard and no problems now.

    I thank everyone for their suggestions and help!

    Chuck

  29. #29
    Registered User
    Join Date
    09-18-2010
    Location
    Sunnyvale, CA
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Protect clipboard data from other programs trying to use clipboard at same time?

    Solved this problem by going at it from a different angle. Jaafar on an other forum wrote some code to protect the clipboard from use by other programs. And then also has some code for other programs to use in their macros to wait until the clipboard is freed up (unprotected) before they try to also use it themselves which would cause an error not being able to copy and likely crash or stop any macro at that point.

    All that got solved with the discussion and code here:

    http://www.mrexcel.com/forum/showthread.php?t=495746

    Thanks for all the help here too, as I used some of the suggestions here too when I did not wish to use the clipboard to copy.

    Chuck

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1