Results 1 to 26 of 26

Thread: [RESOLVED] Copy 2 objects to clipboard at the same time

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Resolved [RESOLVED] Copy 2 objects to clipboard at the same time

    I use this to copy a chart to clipboard: ChartArea.Copy.
    But now we wanna copy two charts at the same time to the clipboard. Is that possible?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    no, you would have to do it by combining the chart areas if that can be done as it can with ranges
    else, can you set the copy destination then just copy the 2 areas
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Pete,

    Can u show me what you mean...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i have never tried with chart areas, but to combine 2 (or more) ranges, use union
    vb Code:
    1. set myrange = union(range("A4:J19"),range("L12:X99"))
    2. myrange.copy
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Thanks Pete,

    But if you do this manually, you select the first chart then hold down Ctrl buttom and select the second chart. Then you can copy and paste two charts. Can't I do this in VB?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i am sure you can if you can do manually, the chart area should just be another range as i posted above
    i try to never use select when i write code as i can have problems, especially from outside programs, so would combine without selecting, then copy
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, I will try that,

    But If I wanna copy (with VB) two charts inside two OLE (OLE, OLE2) objects the union function doesen't work. Have you any suggestion how I should do?

    Copy one chart I do like this
    VB Code:
    1. Dim objGraph As Object
    2.  
    3. Clipboard.Clear
    4.  
    5. Set objGraph = Me.OLE1.object
    6.  
    7. objGraph.ChartArea.Copy

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    someone?

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    When I record a macro in excel and then copy two Object charts it looks like this:
    VB Code:
    1. ActiveSheet.Shapes.Range(Array("Object 2", "Object 1")).Copy
    But how can I do this from VB. I have two OLE with MsGraph in each one of them.
    Copy a chart from one OLE is no problem. But how can I copy both charts at the same time to clipboard?
    Copy one chart I do like this
    VB Code:
    1. Dim objGraph As Object
    2. Clipboard.Clear
    3. Set objGraph = Me.OLE1.object
    4. objGraph.ChartArea.Copy
    5. Set objGraph = Nothing
    Last edited by Pirre001; Jan 7th, 2008 at 02:17 PM.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i believe that is like trying to work in 2 instances of excel, so i don't believe you would be able to do it, the only way i can think of is to copy the first, paste it before of after the second, then copy both, or use a temporary workspace to copy both to, before copying together
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, use a temporary workspace to copy both to is a good ide. But as usual, how should I do that in VB? Do you have any examples?

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    use an empty sheet in one of your workbooks?
    add a new sheet if need be delete when finished
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, but I not use excel. All this is in VB...

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    yes, but you already have workbooks open that you can work with

    a range is copied as a string, so it can just be returned into a string variable, but if you want to position one range against another or anything like that then putting it into another sheet makes sense, but if you ara happy to just have one followed by the second then you could just add both to string variables and manipulate the strings
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, I understand.

    Do you have any complete example how I should do?

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    try like this
    vb Code:
    1. Dim strtxt As String ' delcare as global at top of form or module
    2. Private Sub Command1_Click()   'get 1st clipboard object
    3. strtxt = Clipboard.GetText
    4. End Sub
    5.  
    6. Private Sub Command2_Click()   'get 2nd clipboard object
    7. strtxt = strtxt & vbNewLine & Clipboard.GetText
    8. Clipboard.SetText = strtxt   ' copy combined to clipboard
    9. End Sub
    as you are selecting the source by code, you will not need to use separate subs to combine
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Hi again Pete,

    I can't get it to work. If I do like this it only paste the laste copied chart? Why?
    VB Code:
    1. Private Sub cmdCopyCharts()
    2. Dim strtxt As String
    3. Dim objChart1 As Object
    4. Dim ObjChart2 As Object
    5.  
    6. Clipboard.Clear
    7.  
    8. Set objChart1 = Me.OLE1.object
    9. Set ObjChart2 = Me.OLE2.object
    10.  
    11. objChart1.ChartArea.Copy
    12. strtxt = Clipboard.GetText
    13.  
    14. ObjChart2.ChartArea.Copy
    15. strtxt = strtxt & vbCrLf & Clipboard.GetText
    16.  
    17. Clipboard.SetText strtxt
    18.  
    19. Set objChart1 = Nothing
    20. Set ObjChart2 = Nothing
    21.  
    22. End Sub

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    you want to post a demo project? to test with
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, check this...
    Attached Files Attached Files

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i tried this and it basically works, but you will have to do some adjustments to the picture box sizes, to get the image the way you want it
    you will need 3 pictureboxes, though you could probably use stdpictures for 2

    vb Code:
    1. Set objChart1 = Me.OLE1.object
    2. Set ObjChart2 = Me.OLE2.object
    3. objChart1.chartarea.Copy
    4. Picture2.Picture = Clipboard.GetData
    5. ObjChart2.chartarea.Copy
    6.  
    7. Picture3.Picture = Clipboard.GetData
    8. Clipboard.Clear
    9. Picture1.AutoRedraw = True
    10. Picture1.PaintPicture Picture2.Picture, 0, 0
    11. Picture1.PaintPicture Picture3.Picture, 0, Picture2.Height
    12. Picture1.Refresh
    13. Picture1.Picture = Picture1.Image
    14. Clipboard.SetData Picture1.Picture
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Thanks again Pete!

    Now it almost works for me. But I have problems with the sizes. Can you upload your working sample also?

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i didn't try to get the sizes right, but you can try setting autosize to true for all the pictureboxes, all the pictureboxes could be set to visible to false, a you don't need to see them
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Ok, but I only still get the last copied chart to the clipboard. Not both when i do a paste? Why?

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Pete,

    Can you upload your working project here?

  25. #25
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy 2 objects to clipboard at the same time

    i don't know that i kept it, i will have to redo a bit later
    (close, don't save)

    here it is, works perfectly............... lol
    only 2 pictureboxes required, picture2 needs autosize
    Attached Files Attached Files
    Last edited by westconn1; Jan 27th, 2008 at 06:36 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Copy 2 objects to clipboard at the same time

    Thanks Pete!

Posting Permissions

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



Click Here to Expand Forum to Full Width