Results 1 to 8 of 8

Thread: Load images from separate folders
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Load images from separate folders

    Hi,

    This code is giving me a headache and I need someone fix it for me.

    Currently both controls are loading the image from same folder "Picture1".

    I want the second control to load from "picture2".

    I have tried some stuffs but the script keeps laughing at me .




    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target = Me.[h9] Or Target = Me.[h36] Then
        Select Case Target.Address
            Case Is = "$H$9"
                LoadIm "Image1", Me.[h9]
            Case Is = "$H$36"
                LoadIm "Image2", Me.[h36]
        End Select
    End If
    End Sub
    Sub LoadIm(cname$, r As Range)
    Dim fpath$, sfile$
    fpath = ThisWorkbook.Path & "\Picture1"
    sfile = Dir(fpath & "\" & Right(r.Text, 3) & ".*")
    If sfile <> vbNullString Then
        Me.OLEObjects(cname).Object.Picture = LoadPicture(fpath & "\" & sfile)
    Else
        Me.OLEObjects(cname).Object.Picture = LoadPicture("")
    End If
    If Err.Number = 53 Then Me.OLEObjects(cname).Object.Picture = LoadPicture("")
    End Sub
    There Is Always A Better Way

  2. #2
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,276
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Load images from separate folders

    It can be the following

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target = Me.[h9] Or Target = Me.[h36] Then
            Select Case Target.Address
                Case Is = "$H$9"
                    LoadIm "Image1", Me.[h9], 1
                Case Is = "$H$36"
                    LoadIm "Image2", Me.[h36], 2
            End Select
        End If
    End Sub
    
    
    Sub LoadIm(cname$, r As Range, n As Long)
        Dim fpath$, sfile$
        fpath = ThisWorkbook.Path & "\Picture" & n
        sfile = Dir(fpath & "\" & Right(r.Text, 3) & ".*")
        If sfile <> vbNullString Then
            Me.OLEObjects(cname).Object.Picture = LoadPicture(fpath & "\" & sfile)
        Else
            Me.OLEObjects(cname).Object.Picture = LoadPicture("")
        End If
        If Err.Number = 53 Then Me.OLEObjects(cname).Object.Picture = LoadPicture("")
    End Sub
    Regards Dante Amor

  3. #3
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load images from separate folders

    Cool !!!

    But one thing is :

    The real folders don't have such naming convention.

    I never taught those sample I gave will trap me like this.

    The folder names are
    1. Passport
    2. Signature

    Sorry for not being clear at first
    There Is Always A Better Way

  4. #4
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,276
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Load images from separate folders

    Then send as parameter the full name

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target = Me.[h9] Or Target = Me.[h36] Then
            Select Case Target.Address
                Case Is = "$H$9"
                    LoadIm "Image1", Me.[h9], "Passport"
                Case Is = "$H$36"
                    LoadIm "Image2", Me.[h36], "Signature"
            End Select
        End If
    End Sub
    
    
    
    
    Sub LoadIm(cname$, r As Range, folder As String)
        Dim fpath$, sfile$
        fpath = ThisWorkbook.Path & "\" & folder
        sfile = Dir(fpath & "\" & Right(r.Text, 3) & ".*")
        If sfile <> vbNullString Then
            Me.OLEObjects(cname).Object.Picture = LoadPicture(fpath & "\" & sfile)
        Else
            Me.OLEObjects(cname).Object.Picture = LoadPicture("")
        End If
        If Err.Number = 53 Then Me.OLEObjects(cname).Object.Picture = LoadPicture("")
    End Sub
    Regards Dante Amor

  5. #5
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load images from separate folders

    Quote Originally Posted by DanteAmor View Post
    Then send as parameter the full name

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target = Me.[h9] Or Target = Me.[h36] Then
            Select Case Target.Address
                Case Is = "$H$9"
                    LoadIm "Image1", Me.[h9], "Passport"
                Case Is = "$H$36"
                    LoadIm "Image2", Me.[h36], "Signature"
            End Select
        End If
    End Sub
    
    
    
    
    Sub LoadIm(cname$, r As Range, folder As String)
        Dim fpath$, sfile$
        fpath = ThisWorkbook.Path & "\" & folder
        sfile = Dir(fpath & "\" & Right(r.Text, 3) & ".*")
        If sfile <> vbNullString Then
            Me.OLEObjects(cname).Object.Picture = LoadPicture(fpath & "\" & sfile)
        Else
            Me.OLEObjects(cname).Object.Picture = LoadPicture("")
        End If
        If Err.Number = 53 Then Me.OLEObjects(cname).Object.Picture = LoadPicture("")
    End Sub
    Wow.

    I can't believe this! !!

    Can you please explain the logic behind the code for me?

    Especially that folder thing.

    There Is Always A Better Way

  6. #6
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,276
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Load images from separate folders

    This routine receives 3 parameters:
    Sub LoadIm (cname $, r As Range, As String folder)
    The name of the form, the range and the name of the folder.


    For example: LoadIm "Image1", Me. [H9], "Passport"


    This time you are sending the data "Passport".


    And in this line uses that data to complete the folder
    fpath = ThisWorkbook.Path & "" & folder
    Regards Dante Amor

  7. #7
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load images from separate folders

    Okay. Thanks for your patience.
    There Is Always A Better Way

  8. #8
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,276
    Post Thanks / Like
    Mentioned
    72 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Load images from separate folders

    No problem. Let me know if you have any doubt.
    Regards Dante Amor

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •