Load images from separate folders

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
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:LOL: .




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$
[COLOR=#ff0000][B]fpath = ThisWorkbook.Path & "\Picture1"[/B][/COLOR]
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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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][COLOR=#0000ff], 1[/COLOR]
            Case Is = "$H$36"
                LoadIm "Image2", Me.[h36][COLOR=#0000ff], 2[/COLOR]
        End Select
    End If
End Sub


Sub LoadIm(cname$, r As Range,[COLOR=#0000ff] n As Long[/COLOR])
    Dim fpath$, sfile$
    fpath = ThisWorkbook.Path & [COLOR=#0000ff]"\Picture" & n[/COLOR]
    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
 
Upvote 0
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:LOL:.

The folder names are
1. Passport
2. Signature

Sorry for not being clear at first
 
Upvote 0
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], [COLOR=#0000ff]"Passport"[/COLOR]
            Case Is = "$H$36"
                LoadIm "Image2", Me.[h36], [COLOR=#0000ff]"Signature[/COLOR]"
        End Select
    End If
End Sub




Sub LoadIm(cname$, r As Range, [COLOR=#0000ff]folder As String[/COLOR])
    Dim fpath$, sfile$
    fpath = ThisWorkbook.Path &[COLOR=#0000ff] "\" & folder[/COLOR]
    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
 
Upvote 0
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], [COLOR=#0000ff]"Passport"[/COLOR]
            Case Is = "$H$36"
                LoadIm "Image2", Me.[h36], [COLOR=#0000ff]"Signature[/COLOR]"
        End Select
    End If
End Sub




Sub LoadIm(cname$, r As Range, [COLOR=#0000ff]folder As String[/COLOR])
    Dim fpath$, sfile$
    fpath = ThisWorkbook.Path &[COLOR=#0000ff] "\" & folder[/COLOR]
    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.

:confused:
 
Upvote 0
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
 
Upvote 0
No problem. Let me know if you have any doubt.
 
Upvote 0

Forum statistics

Threads
1,213,568
Messages
6,114,348
Members
448,570
Latest member
rik81h

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