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:laugh: .




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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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:laugh:.

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

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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