Results 1 to 4 of 4

Thread: Charts in Userform

Hybrid View

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Unhappy Charts in Userform

    lets say in sheet1 we have the following values

    A1="NAMES" B1="SCORE"
    A2="FRANK" B2="10"
    A3="JACK" B3="20"
    A4="JOE" B4="30"

    When i click the command button in userform1 then the chart should be dispayed for the above data in the same userform. the problem is...

    How do I achieve it???? :-(

    I tried using the microsoft chart control but it asks me to set a connection to a query or any other database. i don't want to use this. i have gone through the microsoft help but didn't get any answer. is there any other way?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Charts in Userform

    You can do this using API calls to the clipboard. Here's the code and a sample workbook, showing how this works.
    You will need to add a reference to the "OLE Automation" type library in order to access the IPicture object.
    VB Code:
    1. Option Explicit
    2.  
    3. 'Requires a reference to the "OLE Automation" type library
    4.  
    5. '----------------------------------------------------------------------------
    6. ' User-Defined Type for API Calls
    7. '----------------------------------------------------------------------------
    8.  
    9. 'Declare a Type to store a GUID for the IPicture OLE Interface
    10. Private Type GUID
    11.     Data1 As Long
    12.     Data2 As Integer
    13.     Data3 As Integer
    14.     Data4(0 To 7) As Byte
    15. End Type
    16.  
    17. 'Declare a Type to store the image information
    18. Private Type uPicDesc
    19.     Size As Long
    20.     Type As Long
    21.     hPic As Long
    22.     hPal As Long
    23. End Type
    24.  
    25.  
    26. '----------------------------------------------------------------------------
    27. 'Windows API Function Declarations
    28. '----------------------------------------------------------------------------
    29.  
    30. 'Does the clipboard contain a Metafile Picture?
    31. Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
    32.  
    33. 'Open the Clipboard
    34. Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    35.  
    36. 'Get a handle on the Picture
    37. Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
    38.  
    39. 'Create a copy of the metafile
    40. Private Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
    41.  
    42. 'Close the clipboard
    43. Private Declare Function CloseClipboard Lib "user32" () As Long
    44.  
    45. 'Convert the handle into an OLE IPicture interface.
    46. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    47.  
    48.  
    49. Function PastePicture() As IPicture
    50.  
    51. Const lMETAFILE As Long = 14
    52.  
    53. Dim lPictureAvailable As Long
    54. Dim lClipHandle As Long
    55. Dim lPicHandle As Long
    56. Dim lCopyHandle As Long
    57. Dim uInterGUID As GUID
    58. Dim uPictureInfo As uPicDesc
    59. Dim lOLEHandle As Long
    60. Dim iTempPicture As IPicture
    61.  
    62.     'Check if the clipboard contains a picture file
    63.     lPictureAvailable = IsClipboardFormatAvailable(lMETAFILE)
    64.    
    65.     If lPictureAvailable <> 0 Then
    66.        
    67.         'Get a Handle on the Clipboard
    68.         lClipHandle = OpenClipboard(0&)
    69.        
    70.         If lClipHandle > 0 Then
    71.            
    72.             'Get a Handle on the Picture
    73.              lPicHandle = GetClipboardData(lMETAFILE)
    74.              
    75.              'Make a local copy, in case the clipboard is changed
    76.              lCopyHandle = CopyEnhMetaFile(lPicHandle, vbNullString)
    77.              
    78.             'Release Handle from Clipboard
    79.             lClipHandle = CloseClipboard
    80.            
    81.             'Only Continue if we have a handle on the Picture
    82.             If lPicHandle <> 0 Then
    83.                
    84.                 ' Create the Interface GUID (for the IPicture interface)
    85.                 With uInterGUID
    86.                     .Data1 = &H7BF80980
    87.                     .Data2 = &HBF32
    88.                     .Data3 = &H101A
    89.                     .Data4(0) = &H8B
    90.                     .Data4(1) = &HBB
    91.                     .Data4(2) = &H0
    92.                     .Data4(3) = &HAA
    93.                     .Data4(4) = &H0
    94.                     .Data4(5) = &H30
    95.                     .Data4(6) = &HC
    96.                     .Data4(7) = &HAB
    97.                 End With
    98.  
    99.                 ' Fill UPictureInfo with necessary parts.
    100.                 With uPictureInfo
    101.                     .Size = Len(uPictureInfo)   ' Length of structure.
    102.                     .Type = 4                   ' Type of Picture = Metafile
    103.                     .hPic = lCopyHandle         ' Handle to image.
    104.                     .hPal = 0                   ' Handle to palette.
    105.                 End With
    106.                
    107.                 'Create the IPicture Object
    108.                 lOLEHandle = OleCreatePictureIndirect(uPictureInfo, uInterGUID, True, iTempPicture)
    109.                
    110.                 If lOLEHandle = 0 Then
    111.                     Set PastePicture = iTempPicture
    112.                 End If
    113.             End If
    114.         End If
    115.     End If
    116. End Function
    Attached Files Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Charts in Userform

    As usual, u have come to help me again thanks

    I'll try this right away.

  4. #4
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: Charts in Userform

    I know this is an old forum, but this sub looks like the answer I am looking for, but what I need it to do is copy and paste a picture which I inserted in Excel 2010. I tried to modify the code, but not having any luck. Thanks.

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