Results 1 to 8 of 8

Displaying clipboard contents (image) to a userform

This is a discussion on Displaying clipboard contents (image) to a userform within the Excel Questions forums, part of the Question Forums category; I am looking to display a picture currently copied to the clipboard in an image object in a userform. The ...

  1. #1
    New Member
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    33

    Default Displaying clipboard contents (image) to a userform

    I am looking to display a picture currently copied to the clipboard in an image object in a userform.

    The picture in the clipboard get there in the following way :
    Code:
    pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Any ideas?

    Thanks,
    adam

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    69,411

    Default Re: Displaying clipboard contents (image) to a userform

    Private Sub CommandButton1_Click()
    Const FName As String = "C:\TempChart.gif"
    Dim pic_rng As Range
    Dim ShTemp As Worksheet
    Dim ChTemp As Chart
    Dim PicTemp As Picture
    Application.ScreenUpdating = False
    Set pic_rng = Worksheets("Sheet1").Range("A1:B3")
    Set ShTemp = Worksheets.Add
    Charts.Add
    ActiveChart.Location Where:=xlLocationAsObject, Name:=ShTemp.Name
    Set ChTemp = ActiveChart
    pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    ChTemp.Paste
    Set PicTemp = Selection
    With ChTemp.Parent
    .Width = PicTemp.Width + 8
    .Height = PicTemp.Height + 8
    End With
    ChTemp.Export Filename:="C:\TempChart.gif", FilterName:="gif"
    UserForm1.Image1.Picture = LoadPicture(FName)
    Kill FName
    Application.DisplayAlerts = False
    ShTemp.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub
    [/code]

    The code copies the picture to a new blank Chart, exports the Chart and uses the resultant GIF file as the source for the Image Control.

  3. #3
    New Member
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    33

    Default Re: Displaying clipboard contents (image) to a userform

    Thanks for your reply Andrew.

    Is there a way to avoid saving the image in the clipboard to a file or pasting it into a chart between the clipboard and the image object on the userform?

    I fiddled around trying to get the image from the clipboard into a DataObject or general Object then trying to set the picture property of the image object on my userform to that object but no success. It may not be possible, or (far more likely), i am not using these objects properly.

    Thanks,
    adam

  4. #4
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    69,411

    Default Re: Displaying clipboard contents (image) to a userform

    Quote Originally Posted by adampease

    Is there a way to avoid saving the image in the clipboard to a file or pasting it into a chart between the clipboard and the image object on the userform?

    Thanks,
    adam
    You can manually paste from the clipboard into the Picture property, but I don't think you can do it in code. Help says:

    "While designing a form, you can use the control's property page to assign a bitmap to the Picture property. While running a form, you must use the LoadPicture function to assign a bitmap to Picture."

    That's why I suggested my workaround.

  5. #5
    New Member
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    33

    Default Re: Displaying clipboard contents (image) to a userform

    Okay.

    Thanks Andrew. Much Appreciated.

    adam

  6. #6
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    11,008

    Default Re: Displaying clipboard contents (image) to a userform

    How did the picture get on the clipboard in the first place? If that code line in your first post is the result of you selecting the picture by browsing through an Open dialog box and selecting the graphic file, this code would load that selected picture onto the Image control and bypass the Clipboard.

    Private Sub CommandButton1_Click()
    Dim MyImage As String
    MyImage = Application.GetOpenFilename()
    If MyImage <> "False" Then Image1.Picture = LoadPicture(MyImage)
    Me.Repaint
    End Sub

  7. #7
    New Member
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    33

    Default

    "pic_rng" in the following is a range within excel :

    Code:
    pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    CopyPicture is descripbed in Excel's Help as :
    "Copies the selected object to the Clipboard as a picture."

    The selected object in this case is a range of cells.

    What i've done so far is build a macro that will, essentially, take a picture of the selected range of cells and the user can then choose to save that picture as a jpeg or gif but also leave it on the clipboard to paste into other applications. I wanted to have an interface pop up after the user chooses the range of cells and on this interface would be a 'preview' of the image that could then be saved or left on the clipboard.

    adam[/code]

  8. #8
    Board Regular
    Join Date
    Jul 2004
    Location
    United States
    Posts
    60

    Default

    Stephen Bullen as a solution that works great;

    http://www.oaltd.co.uk/Excel/SBXLPage.asp#VBA

    look for "pastepicture.zip". The only problems I have had using it come when running windows through a shell (off of a non-windows system) and the shell cannot handle the clipboard properly , so an error forms when the function tries to call the clipboard (i.e., not really a problem with the code, just the situation to use it). If you are using this on a normal PC, then its exactly what you want I think and much faster than J Walkenbach's code (exporting the ".gif" file).

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com