Share via

for each userform in vbcomponents

Anonymous
2012-11-24T09:30:25+00:00

Hi I want to change the graphic in each image control on each form, but I've never programmed the VBcomponents before and I'm struggling...

I want something like:

Dim xForm As MSForms.UserForm

Dim xControl As MSForms.Control

For Each xForm In ThisWorkbook.VBProject.UserForms

For Each xControl In xForm.Controls

If xControl.Name = "imgTSC" Then

xControl.Picture -"C:\Users\Michael\LOGOS\TSC_small.jpg"

End If

Next xControl

Next xForm

But of course this doesn't work... Any ideas how can I achieve it, or is there an MVP page that explains it all for a simpleton like me?

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2012-11-24T16:11:02+00:00

    Hi Thanks, but my problem is not with LoadPicture, but with how to reference the userforms class to use with a 'For Next' loop

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2012-11-24T12:00:35+00:00

    Remember to load all forms you need (Load UserForm1, Load UserForm2) and then try to use this code:

    <CODE>

    For Each xForm In UserForms

        For Each xControl In xForm.Controls

            If xControl.Name = "imgTSC" Then

                xControl.Picture = LoadPicture("C:\Users\Michael\LOGOS\TSC_small.jpg")

            End If

        Next xControl

    Next xForm

    </CODE>

    You can read more about LoadPicture Function at http://msdn.microsoft.com/en-us/library/aa264946(v=vs.60).aspx website.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  3. Anonymous
    2012-11-25T06:26:03+00:00

    I had not been loading the userforms, so my 'UserForms' class was empty. However, in the meantime, I have found a way of doing it without loading them:

    For archive purposes and to leave the string of this question complete, I will add the code tha - with your help and much reading - I have finally got to work below...

    Dim xForm As Object, xControl As Object

    Dim MyFileAndPath As Variant

    MyFileAndPath = Application.GetOpenFilename("Select picture type file (*.bmp; *.gif; *.jpg; *.jpeg; *.wmf; *.emf)" _

    & ",*.bmp;*.gif;*.jpg;*.jpeg;*.wmf;*.emf")

    If MyFileAndPath = False Then End

    For Each xForm In ThisWorkbook.VBProject.VBComponents

    If LCase(Left(xForm.Name, 3)) = "frm" Then

    For Each xControl In xForm.designer.Controls

    If xControl.Name = "imgTSC" Then

    xControl.Picture = LoadPicture(MyFileAndPath)

    End If

    Next xControl

    End If

    Next xForm

    (P.S.all my forms are named with the prefix 'frm')

    Thanks

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-11-24T18:20:46+00:00

    You should not use For Each xForm In ThisWorkbook.VBProject.UserForms in VBA code. Instead you should use For Each xForm In UserForms.

    If you have more than one userform, you should set ShowModal Property to false or load these forms and/or show them in modeless mode  (to get them all in UserForms collection), like this:

    <CODE>

    Load UserForm1

    Load UserForm2

    UserForm1.Show vbModeless

    UserForm2.Show vbModeless

    </CODE>

    and then you can use a 'For Next' loop.

    You can read more at http://support.microsoft.com/kb/829070 and http://support.microsoft.com/kb/207714.

    Was this answer helpful?

    0 comments No comments