1

I am getting a runtime error 91 for below code...I dont know what mistake I am making but it isn't working. Thanks in Advance.

Dim Pict As String
Dim ImgFileFormat As String

Dim img As Object

ImgFileFormat = _
"Image Files (*.bmp;*.gif;*.tif;*.jpg),*bmp;*gif;*.tif;*.jpg"


GetPict:
Pict = Application.GetOpenFilename(ImgFileFormat)

If Pict = "False" Then Exit Sub

'91 error here -v
Set img = ThisWorkbook.VBProject.VBComponents("UserForm10").Designer.Controls("Image3").Picture 

With img
    .LoadPicture (Pict)
    .PictureSizeMode = fmPictureSizeModeStretch
End With
Vityata
  • 42,633
  • 8
  • 55
  • 100
Adil
  • 21
  • 1
  • If your code is inside `UserForm10`, change the set img to this - `Set img = me.Controls("Image3").Picture` If not, give more info - where is the code, what is the Sub's name? – Vityata May 24 '18 at 10:21
  • Possible duplicate of [VBA: Run time error '91'?](https://stackoverflow.com/questions/18927297/vba-run-time-error-91) – Peter Abolins May 24 '18 at 10:22
  • @PeterAbolins - the OP has the word `set` on the question. – Vityata May 24 '18 at 10:24
  • 1
    @Vityata... thanks a lottt for editing the question (this is my second question)...and thanks for the prompt response...but that isnt working...and the code is inside the Userform10. and the sub name is "Private Sub Label7_Click()" – Adil May 24 '18 at 10:37
  • @Vityata... this code `Me.Controls("Image3").Picture = LoadPicture(Pict)` works for me but the problem with this code is that the picture in the userform doesnt get saved, but uploads it and the next time the default picture is still there and not the uploaded one... – Adil May 24 '18 at 10:38
  • @Adil - yup, but this is quite a separate problem, not the `91`. In general, a way to do it is to save the `Image` path in a specific excel cell and to refer to it whenever you are loading the form. – Vityata May 24 '18 at 10:53
  • @Vityata...so it isnt possible to load directly into the userform?? – Adil May 24 '18 at 10:54
  • @Vityata...sorry...im new to this...and thanks for your support.... – Adil May 24 '18 at 10:55
  • @Adil - in general, the `.LoadPicture` does not keep the picture, as you have noticed. There could be another way to do it, I don't know. The refering to a specific cell in excel is just the way that I know. – Vityata May 24 '18 at 10:57

1 Answers1

0

Remove the word .Picture from your code here:

`Set img = ThisWorkbook.VBProject.VBComponents("UserForm10").Designer.Controls("Image3").Picture`

This is because .Picture is a String property of an Image and it should not be set. See: Picture Property MSDN

Depending on what you are doing, there are different options. If your code is in a userform, and you are not following the MVC pattern, something like this changes the picture, upon a click on Label7 of the form:

Private Sub Label7_Click()

    Dim img As Object
    Set img = Me.Controls("Image3")

    Dim picAddress As String
    picAddress = Application.GetOpenFilename
    img.Picture = LoadPicture(picAddress)

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • The code is loading the picture to the userform...but it isn't saving it in the `Userform`...so the next time the userform is loaded the uploaded picture isnt available. – Adil May 24 '18 at 10:51
  • Im trying to develop a `Userform` where the end user would be able to upload a pic and have it in the userform. – Adil May 24 '18 at 10:53
  • @Adil - saving the picture to the Userform is a different problem than the `91`. In general, a way to do it is to save the Image path in a specific excel cell and to refer to it whenever you are loading the form. – Vityata May 24 '18 at 10:54
  • @Vityata...Ohhhk....thanks a lottt...will use the other approach as you mentioned...and sorry for the trouble and inconvenience...May God Bless You... :) – Adil May 24 '18 at 10:58
  • @Adil - no problem & no trouble at all, you are welcome :) – Vityata May 24 '18 at 10:59