View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Roger on Excel Roger on Excel is offline
external usenet poster
 
Posts: 249
Default putting a picture onto a userform without using the vba window

Dear Peter,

Its looking really good - i was able to copy the code and have two forms
with different pictures on each.

However, could you describe how to save the changes in a little more detail
as I would need the user to be able to save their changes from session to
session?

I pasted the following into ThisWorkbook

ThisWorkbook.Worksheets("Sheet1").OLEObjects("Imag e1").Object.Picture = _
Me.Image1.Picture

But it said that it was invalid outside a procedure - is there some other
code needed?

Roger

"Peter T" wrote:

Glad it seems to be working, after all that!

A question I have is how does one make it so that it so that the picture
stays on the form when I reopen it? When I close and reopen the form, the
picture disappears.


If the requirement is only for the session, in the form -

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = True
Me.Hide
End If

One way to permanently store the image would be to an ActiveX Image control
(hidden) in a sheet, eg

ThisWorkbook.Worksheets("Sheet1").OLEObjects("Imag e1").Object.Picture = _
Me.Image1.Picture

reverse to load back to the form's image control

Another way would be to save to file and load from file.

I also noticed that if I draw a selection box in a pdf and copy the
selection to the clipboard,


Oops, my fault, not sure how such an elementary error crept in!
In the userform code (as posted last time) replace CommandButton1_Click with
the following

Private Sub CommandButton1_Click()
Dim lPicType As Long, lXlPicType As Long
lPicType = WhatsInClipboard

If lPicType = 1 Then
lXlPicType = xlBitmap

ElseIf lPicType = 2 Then
lXlPicType = xlPicture
ElseIf lPicType = 3 Then
res = MsgBox("BMP & EMF available" & vbCr & _
"press Yes for BMP, No for EMF", vbYesNoCancel)
If res = vbYes Then
lXlPicType = xlBitmap
ElseIf res = vbNo Then
lXlPicType = xlPicture
Else
Exit Sub
End If

Else
MsgBox "No picture on clipboard"
Exit Sub
End If

Me.Image1.Picture = PastePicture(lXlPicType)
End Sub

Trust you've got the image control's picture properties adjusted to needs,
either at design or amended at runtime.. FWIW, if you want to know the
overall dimensions of the image before showing it (first make invisible),
allow the image to Autosize, get the new dim's, resize to original or other
as required, or leave as autosize'd

Regards,
Peter T


"Roger on Excel" wrote in message
...
Hi Peter,

Many thanks for helping me on this problem.

I tried your code out and it works very very nicely - It puts a ChemDraw
structure nicely onto the form. It also puts copied images in to the
image
box (which will be especially useful when cutting and pasting from other
documents/sources other than chemdraw)

A question I have is how does one make it so that it so that the picture
stays on the form when I reopen it? When I close and reopen the form, the
picture disappears.

In my spreadsheet I have 10 different forms which the user calls up and
each
one will need to have a different chemical structure associated with and
showing on it.

I also noticed that if I draw a selection box in a pdf and copy the
selection to the clipboard, it doesnt paste the selection into the form.
Is
there a way to do this as sometimes a user may elect to copy a structure
from
a pdf using the marquee tool in adobe reader and paste that (for example;
if
they dont have Chemdraw).

Roger




"Peter T" wrote:

Have a go with the following. Put a button and and image control on a
form.
For testing
suggest show the form modeless

Sub ShowForm()
UserForm1.Show vbModeless
End Sub

Run the form, activate Excel, select some cells, (in Excel97-2003) hold
Shift and select Edit - CopyPicture, try both Picture and Bitmap. In
Excel2007 click the arrow below Paste, As picture, Copy Picture (but note
emf/picture does not work correctly in 2007 due a bug).

For your eventual purposes I'm a bit concerned about your overal
arrangement, in particular how do you know user has copied a suitable
picture before opening your workbook (which could destroy the clipboard
depending on other factors). To at least get some idea I've added an
additional function WhatsInClipboard (see code) which will at least tell
you
a picture is available, although not if it is an appropriate one. I
suspect
you will only want 'bitmap' but if your ChemDraw app supports it the
'metafile' may give a better result. Either way eventually you probably
won't want to be asking the user what type (as below).

Obviously if you want the image to automatically appear when the form
loads,
call the PastePicture from the form's initialize event (be sure to pass
the
intended picture type).

Finally, you will probably want to tinker with the Image controls picture
properties, either at design or during runtime (see comments).

'' In a UserForm with CommandButton1 and Image1
'' the image control's Autosize, PictureAlignment and PictureSizeMode
'' can be fixed at design or during runtime

Private Sub CommandButton1_Click()
Dim lPicType As Long, lXlPicType As Long
lPicType = WhatsInClipboard

If lPicType = 1 Then
lXlPicType = xlBitmap

ElseIf lPicType = 2 Then
lXlPicType = xlPicture

If lPicType = 3 Then
res = MsgBox("BMP & EMF available" & vbCr & _
"press Yes for BMP, No for EMF", vbYesNoCancel)
If res = vbYes Then
lXlPicType = xlBitmap
ElseIf res = vbNo Then
lXlPicType = xlPicture
Else
Exit Sub
End If
End If
Else
MsgBox "No picture on clipboard"
Exit Sub
End If

Me.Image1.Picture = PastePicture(lXlPicType)
End Sub

'''' In a Normal Module

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''
'' Based almost entirely on Stephen Bullen's "PastePicture.Zip"
'' available from http://www.oaltd.co.uk
'' Code below is copied from modPastePicture with headers and some
'' comments removed. Function fnOLEError in the original module
'' is also removed but shold be added back for completion.
''
'' An additional new function - WhatsInClipboard() is included
''
'' Recommend obtain the original module and include WhatsInClipboard
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''

Option Explicit
Option Compare Text

''' User-Defined Types for API Calls

'Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type

'Declare a UDT to store the bitmap information
Private Type uPicDesc
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type

'''Windows API Function Declarations

'Does the clipboard contain a bitmap/metafile?
Private Declare Function IsClipboardFormatAvailable Lib "user32" ( _
ByVal wFormat As Integer) As Long

'Open the clipboard to read
Private Declare Function OpenClipboard Lib "user32" ( _
ByVal hwnd As Long) As Long

'Get a pointer to the bitmap/metafile
Private Declare Function GetClipboardData Lib "user32" ( _
ByVal wFormat As Integer) As Long

'Close the clipboard
Private Declare Function CloseClipboard Lib "user32" () As Long

'Convert the handle into an OLE IPicture interface.
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
PicDesc As uPicDesc, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

'Create our own copy of the metafile, so it doesn't get _
'wiped out by subsequent clipboard updates.
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" ( _
ByVal hemfSrc As Long, ByVal lpszFile As String) As Long

'Create our own copy of the bitmap, so it doesn't get wiped out by
subsequent
'clipboard updates.
Declare Function CopyImage Lib "user32" (ByVal handle As Long, _
ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, _
ByVal un2 As Long) As Long

'The API format types we're interested in
Const CF_BITMAP = 2
Const CF_PALETTE = 9
Const CF_ENHMETAFILE = 14
Const IMAGE_BITMAP = 0
Const LR_COPYRETURNORG = &H4

Function WhatsInClipboard() As Long
' This function is not included in PastePicture.zip
' Purpose is to learn which usable picture formats are in the
' clipboard, if any, before doing 'PastePicture'

Dim hPicAvail As Long
Dim picTypes As Long

If IsClipboardFormatAvailable(CF_BITMAP) Then
picTypes = 1
End If
If IsClipboardFormatAvailable(CF_ENHMETAFILE) Then
picTypes = picTypes Or 2
End If

WhatsInClipboard = picTypes
' 1 got a bitmap
' 2 got a metafile
' 3 got both
' 0 got neither

End Function


Function PastePicture(Optional lXlPicType As Long = xlPicture) As
IPicture
'Some pointers
Dim h As Long, hPicAvail As Long, hPtr As Long, hPal As Long
Dim lPicType As Long, hCopy As Long

'Convert xl piture-type constant to the API constant equivalent
lPicType = IIf(lXlPicType = xlBitmap, CF_BITMAP, CF_ENHMETAFILE)

'Check if the clipboard contains the required format
hPicAvail = IsClipboardFormatAvailable(lPicType)

If hPicAvail < 0 Then
'Get access to the clipboard
h = OpenClipboard(0&)

If h 0 Then
'Get a handle to the image data
hPtr = GetClipboardData(lPicType)

'Create our own copy of the image on the clipboard, in the appropriate
format.
If lPicType = CF_BITMAP Then
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Else
hCopy = CopyEnhMetaFile(hPtr, vbNullString)