Inserting pictures in UserForm from within workbook

daniel_dani95

New Member
Joined
Jan 30, 2023
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I'm working on a multiple choice quiz for various subjects. Some of the questions have figures that go with them without which you can't solve the question. The problem I have is that i'm not sure how to specify picture path since it is intended to be sent to people who will download the file in various locations (which changes the specified path).

Now, my idea was to use worksheet which has all the figures listed with their names and then when excel generates random questions, if they have figure with them, VLOOKUP searches the table and brings back the picture. But I can't seem to be able to load the picture automatically into UserForm (as all the other operations are going in the background and only test form is visible to the user). I've tried with
VBA Code:
Image1.Picture=Sheet1.Range("Z2").Value
and
VBA Code:
Image1.Picture=Sheet1.Range("Z2").Picture
but nothing happens when i open the user form again, picture box is still empty.

Can anyone help with this as it's the only thing left to do before i have full working test
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Now, my idea was to use worksheet which has all the figures listed with their names and then when excel generates random questions, if they have figure with them, VLOOKUP searches the table and brings back the picture.

That idea would work but you have to save the chosen picture as a local .jpg file and then use the native LoadPicture function to load the file to the userform image control.

Suppose all the pictures are on a sheet named "Pictures" and the result of the VLOOKUP in cell Z2 on the same sheet is the name of the picture to be loaded, the following userform code would load the picture to the image control. In this example I'm using a command button Click to show the picture.

VBA Code:
Private Sub cmdShowPicture_Click()
  
    Dim picShape As Shape
    Dim tempImageFile As String
  
    With Worksheets("Pictures")
        Set picShape = .Shapes(.Range("Z2").Value)
    End With
  
    tempImageFile = Environ("temp") & "\image.jpg"
    Save_Object_As_Picture picShape, tempImageFile
  
    Me.Image1.Picture = LoadPicture(tempImageFile)
  
    Kill tempImageFile
      
End Sub


Private Sub Save_Object_As_Picture(saveObject As Object, imageFileName As String)

    'Save a picture of an object as a JPG/JPEG file
  
    'Arguments
    'saveObject    - any object type whose class has the CopyPicture method: Chart, ChartObject, ChartObjects, OLEObject, OLEObjects, Range, or Shape.
    'imageFileName - the .jpg or .jpeg file name (including folder path if required) the picture will be saved as.
  
    Dim temporaryChart As ChartObject
   
    Application.ScreenUpdating = False
    saveObject.CopyPicture xlScreen, xlPicture
    Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width, saveObject.Height)
    With temporaryChart
        .Activate
        .Chart.Paste
        .Chart.Export imageFileName
        .Delete
    End With
    Application.ScreenUpdating = True
    Set temporaryChart = Nothing
  
End Sub
 
Upvote 0
That idea would work but you have to save the chosen picture as a local .jpg file and then use the native LoadPicture function to load the file to the userform image control.

Suppose all the pictures are on a sheet named "Pictures" and the result of the VLOOKUP in cell Z2 on the same sheet is the name of the picture to be loaded, the following userform code would load the picture to the image control. In this example I'm using a command button Click to show the picture.

VBA Code:
Private Sub cmdShowPicture_Click()
 
    Dim picShape As Shape
    Dim tempImageFile As String
 
    With Worksheets("Pictures")
        Set picShape = .Shapes(.Range("Z2").Value)
    End With
 
    tempImageFile = Environ("temp") & "\image.jpg"
    Save_Object_As_Picture picShape, tempImageFile
 
    Me.Image1.Picture = LoadPicture(tempImageFile)
 
    Kill tempImageFile
     
End Sub


Private Sub Save_Object_As_Picture(saveObject As Object, imageFileName As String)

    'Save a picture of an object as a JPG/JPEG file
 
    'Arguments
    'saveObject    - any object type whose class has the CopyPicture method: Chart, ChartObject, ChartObjects, OLEObject, OLEObjects, Range, or Shape.
    'imageFileName - the .jpg or .jpeg file name (including folder path if required) the picture will be saved as.
 
    Dim temporaryChart As ChartObject
  
    Application.ScreenUpdating = False
    saveObject.CopyPicture xlScreen, xlPicture
    Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width, saveObject.Height)
    With temporaryChart
        .Activate
        .Chart.Paste
        .Chart.Export imageFileName
        .Delete
    End With
    Application.ScreenUpdating = True
    Set temporaryChart = Nothing
 
End Sub
My Z2 Cell gives the picture itself, i presume that would create a problem with the code you wrote. Should I fix that and put just the figure name into Z2 cell and then try with this?
 
Upvote 0
Maybe something like what's suggested here (note thread title).
 
Upvote 0
Maybe something like what's suggested here (note thread title).
my problem is that pictures shoud be loaded with questions, so sometimes question 1 will have figure 1, sometimes figure 31 and sometimes no figure
 
Upvote 0
sometimes question 1 will have figure 1, sometimes figure 31 and sometimes no figure
If you're saying that sometimes the same question uses different pics then that would require references that tie pics to question, similar to how relational databases work. However, you would also need a means of choosing from the pics related to a question by random means if there is more than one?

If you're saying one time #1 is question A and another time #1 is question B then those numbers are just list numbers. They don't have any relationship to a particular question so that is a fundamental design problem I'd say. If you have 50 questions, each one should have an id (1 thru 50 if you prefer) and each number should have **a related pic**. When you present the list, the list numbers are only for ordering the questions, nothing else.

** - or if more than one, again, you'd need a sub list for pics that are for the same exact question and a way to choose one or the other.
HTH
 
Upvote 0
If you're saying that sometimes the same question uses different pics then that would require references that tie pics to question, similar to how relational databases work. However, you would also need a means of choosing from the pics related to a question by random means if there is more than one?

If you're saying one time #1 is question A and another time #1 is question B then those numbers are just list numbers. They don't have any relationship to a particular question so that is a fundamental design problem I'd say. If you have 50 questions, each one should have an id (1 thru 50 if you prefer) and each number should have **a related pic**. When you present the list, the list numbers are only for ordering the questions, nothing else.

** - or if more than one, again, you'd need a sub list for pics that are for the same exact question and a way to choose one or the other.
HTH
There is a question bank loaded in excel worksheet (let's call it TestSheet), in Testsheet there is question column, answers column and figure column. Another sheet (let's call it RandomSheet) then picks random numbers within question bank range (let's say there's 100 questions in bank and test has 30) and displays those questions on the UserForm. That means that sometimes first in UserForm will be random question between 1 and 100 in TestSheet.

I think i will just make all the variations with formula from other thread
 
Upvote 0
Now, my idea was to use worksheet which has all the figures listed with their names and then when excel generates random questions, if they have figure with them, VLOOKUP searches the table and brings back the picture.

What is the layout of the table that VLOOKUP is searching?

My Z2 Cell gives the picture itself, i presume that would create a problem with the code you wrote.

Yes, my code wouldn't work with that setup. How is the picture being put in Z2? What is the code?
Should I fix that and put just the figure name into Z2 cell and then try with this?

If you have the named pictures on a sheet, a table of values and picture names and a VLOOKUP then my code should work. Something like this, with my VLOOKUP in D15 (so the Range("Z2") in my code should be changed to Range("D15"))

Userform VLOOKUP show image from sheet.xlsm
ABCD
11Pictures lookup table
12ValuePicture NamePicture Value
131Pic_000013
142Pic_00002
153Pic_00003Pic_00003
164Pic_00004
175Pic_00005
Pictures
Cell Formulas
RangeFormula
D15D15=VLOOKUP(D13,Table1[#All],2)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top