Change Pic in Image1 when combo box selection changes

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Change Pic in Image1 when combo box selection changes

Post by MSingh »

Hi,

This seems like a far fetched request but:

In userformHelp how can i change
the picture in Image1 when the the selection in combobox1 changes?

Thanks in advance
Mohamed

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change Pic in Image1 when combo box selection changes

Post by HansV »

Let's say that you populate the combo box with a series of file names of pictures in a folder. You can then use code like this:

Code: Select all

Private Sub ComboBox1_Change()
  Me.Image1.Picture = LoadPicture("C:\Test\Pictures\" & Me.ComboBox1)
End Sub
where C:\Test\Pictures is the full path of the folder containing the pictures. The backslash after the path in the code is required.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Change Pic in Image1 when combo box selection changes

Post by MSingh »

Hi Hans,

Thank you for the assistance.

I am trying to adapt J Walkenbach's Excel 2007 Power Programming with VBA
Chapter 24:Providing Help for your Applications - userform3. drop-down list control:

HelpSheet ColumnA has the help "titles". These appear in the combo box.
ColumnB has the explanations of Col A. These populate the frame control, synchronised with the combo box.
I have added ColumnC with pics (made with Irfanview).
How can i synchronise the pics with the combobox?
In this way the pics go with .xlsm.

If this can't be done, I'll go with your first reply-Thank You.

Kind Regards
Mohamed

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change Pic in Image1 when combo box selection changes

Post by HansV »

Go to Office Automation Ltd. - Stephen Bullen's Excel Page and download the file PastePicture.zip.
Extract the workbook PastePicture.xls from the zip file (it doesn't matter where).
Open both your .xlsm workbook and PastePicture.xls.
Activate the Visual Basic Editor.
Drag the module modPastePicture from PastePicture.xls to your workbook; this will copy the module into your workbook.

Now change the event procedure of the combo box as follows:

Code: Select all

Private Sub ComboBox1_Change()
  Dim shp As Shape
  For Each shp In ActiveSheet.Shapes
    If shp.TopLeftCell.Offset(0, -2) = Me.ComboBox1 Then
      shp.CopyPicture xlScreen, xlPicture
      Set Me.Image1.Picture = PastePicture(xlPicture)
      Exit For
    End If
  Next shp
End Sub
This assumes that the top left corner of each picture is over the cell two columns to the right of the cell containing the corresponding title.
x248.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Change Pic in Image1 when combo box selection changes

Post by MSingh »

Hi Hans,

That works 100% - Thank you...
from myself & on behalf of eveyrone else who finds my program user-friendly.

Kindest Regards
Mohamed