• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Invalid Picture while trying to load image in UserForm

ThrottleWorks

Excel Ninja
Hi,

Am using below code to load image in UserForm.
File path is correct. File formant and name is also correct.

However when I try to run this code I get bug saying Invalid Picture at BOLD line. How di I resolve this.
Can anyone please help me in this.

>>> use code - tags <<<
Code:
Sub Add_Dynamic_Image()
    'Add Dynamic Image and assign it to object 'Img'
    Set Img = UserForm2.Controls.Add("Forms.Image.1")
  
    With Img
        'Load Picture to Image Control
        MyPath = "This Path is correct"
'  next line is bolded
        .Picture = LoadPicture(MyPath & "FileName.JPG") 'Change Image Path here
      
        'Align the Picture Size
        .PictureSizeMode = fmPictureSizeModeStretch
      
        'Image Position
        .Left = 50
        .Top = 10
    End With
End Sub
 
Last edited by a moderator:
Hi,​
It could be an invalid path - check with Dir VBA function - or the file can't be loaded under Excel, in this case try another file …​
 
Hi @Marc L sir thanks a lot for the help.
Picture changed, file type changed, same issue, do not know what is happening, re-trying.

Have a nice day ahead. :)
 
Hi @Marc L sir and @vletm it looks like something is wrong with file or image format.
I tried inserting image manually using Image box from Developer's tab (Ribbon option) but got the same pop-up as Invalid Picture.
Thanks a lot for the help. Have a nice weekend. :)

Will revert with more details by tomorrow.
 
Hi,
Can anyone please help me this. I tried various times but somehow it is not working for me.
Just to help you understand in a better way. I am trying to do something like below.

User selects a name from the UserForm List
Based on the selection, next UserForm should show image linked to that name
Along with the image, UserForm will also provide some text-boxes for user input
So one part of the UserForm will have Image, next to it there will be Text-Boxes
So just as example, Image will have name mentioned it, user needs to read it and type in Text-Box
There will 7-8 Tex-Boxes as per the fields

However I am not able to get the image in UserForm. Am stuck at this point only.
 
Without you attaching a workbook for people to test, you can change as needed.
Check and change references where required.
Code:
Private Sub Cmd1_Click()
    Dim strFileName As String
    strFileName = Application.GetOpenFilename(filefilter:="Tiff Files(*.tif;*.tiff),*.tif;*.tiff,JPEG Files (*.jpg;*.jpeg;*.jfif;*.jpe),*.jpg;*.jpeg;*.jfif;*.jpe,Bitmap Files(*.bmp),*.bmp", FilterIndex:=2, Title:="Select a File", MultiSelect:=False)    
    If strFileName = "False" Then
        MsgBox "File Not Selected!"
    Else
        Me.Image1.Picture = LoadPicture(strFileName)
        Image1.PictureSizeMode = fmPictureSizeModeStretch
        Me.Repaint
    End If
End Sub
 
Hi @jolivanes sir, one problem at my end.
When I run your code at my own system it works perfect.
However on my office system (remote environment) this code does not work.
It gives me an error saying invalid picture.
I tried same code in new workbook but same issue.
Path captured by code is correct.
I checked in Immediate Window.
Copied path from Immediate Window and paste in Control + R window of PC to manually open the file.
File is getting opened. UserForm name is correct.
Checked in Immediate Window.
Do not know where the issues is.

Can you please help me in this if you get time.
Have a nice day ahead. :)
 
Hi @jolivanes sir, it is actually my bad. Code is working perfectly outside remote.
I need to check why it is not working in remote. Thanks a lot for the help. Have a nice day ahead. :)
 
Back
Top