+ Reply to Thread
Results 1 to 7 of 7

Loadpicture not working

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2014
    Location
    London
    MS-Off Ver
    MAC 2011
    Posts
    8

    Loadpicture not working

    I'm really struggling with this one! I have a userform which amongst other things contains two Image Placeholders; Image1 and Image2. When I click on an item in the ListBox1 this should then change the images of both placeholders based on what item they have clicked on in the listbox. The code I have placed below works for Image1 but no Image 2 even though it's the same code! For context, the listbox populates other textboxes on the userform. I have defined a range on the sheet as 'myName' which is used for Image1. I have defined another range as 'vicName' which is for Image2. When I click on an item in the listbox the code will then take the value of the listbox and then search for the matching name in a folder found in the workbook path. I'm just very confused as to why this works for one image but not the other! Any suggestions would be greatly appreciated!

    Private Sub listBox1_Click()
          
          Dim EmpFound As Range
          Dim vicFound As Range
          Dim fPath As String
          Dim vfPath As String
    
    With Range("myName")
            Set EmpFound = .Find(ListBox1.Value)
            On Error Resume Next
            If EmpFound Is Nothing Then
                Image1.Picture = LoadPicture(fPath & "nopic.gif")
            Else
                With EmpFound
                    'Look in the directory where this workbook is located.
                    fPath = ThisWorkbook.Path & "\Pictures\"
                    On Error Resume Next
                    'If a matching picture is found then display it.
                    Image1.Picture = LoadPicture(fPath & "\" & ListBox1.Value & ".jpg")
                    'If No picture found then display the default picture.
                    If Err = 0 Then Exit Sub
                    
               Image1.Picture = LoadPicture(fPath & "nopic.gif")
       
       With Range("vicName")
            Set vicFound = .Find(VictimDetailsMain.tbName.Value)
            On Error Resume Next
            If vicFound Is Nothing Then
                Image2.Picture = LoadPicture(vfPath & "nopic.gif")
            Else
                With vicFound
                    vfPath = ThisWorkbook.Path & "\Pictures\Victims"
                    On Error Resume Next
                    Image2.Picture = LoadPicture(vfPath & "\" & VictimDetailsMain.tbName.Value & ".jpg")
                    If Err = 0 Then Exit Sub
             Image2.Picture = LoadPicture(vfPath & "nopic.gif")
       
        Set EmpFound = Nothing
        Set vicFound = Nothing
    
          
     End With
     End If
     End With
     End With
     End If
     End With
    
    End Sub
    EmpFound works, but vicFound doesn't. It won't even load the 'nopic.gif'!

    Any Thoughts?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Loadpicture not working

    Hi,

    You do not assign a value to vfPath unless the lookup value is found, so it is unlikely that your nopic code would ever work- the same applies in fact to both sections of your code.

    Have you verified that VictimDetailsMain.tbName.Value is returning the correct file name? Your liberal use of On Error Resume Next is merely masking errors rather than fixing them and serves mostly to make debugging harder for you.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Loadpicture not working

    I also use Excel 2011 and I'v never been able to get LoadPicture to work. The image controls for Mac excel are very buggy. (Both user form and worksheet images)
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Loadpicture not working

    Suggest putting a break on the Image2. Picure = .....

    and use the Immediate pane to see what vfPath & "\" & VictimDetailsMain.tbName.Value & ".jpg" really evaluates to!

  5. #5
    Registered User
    Join Date
    07-24-2014
    Location
    London
    MS-Off Ver
    MAC 2011
    Posts
    8

    Re: Loadpicture not working

    Thank you all for the suggestions so far. Would someone mind explaining how to use the break point? I've never managed to understand how to use the debugging tool properly!

    I've adjusted the code for both parts to that shown below. The new problem is that it will display a picture now for both images, but if I click on a listbox item that doesn't have a caption associated, then instead of displaying the 'nopic' file it continues to show the last image loaded until I click on an item that does have a caption and an associated file to replace it...any ideas?!

       With Range("vicName")
            Set vicFound = .Find(lblVictimName.Caption)
            vfPath = ThisWorkbook.Path & "\Pictures\Victims\"
            On Error Resume Next
            If vicFound Is Nothing Then
                Image2.Picture = LoadPicture(vfPath & "nopic.gif")
            Else
                With vicFound
                    vfPath = ThisWorkbook.Path & "\Pictures\Victims"
                    'On Error Resume Next
                    Image2.Picture = LoadPicture(vfPath & "\" & lblVictimName.Caption & ".jpg")
                    If Err = 0 Then
                    Image2.Picture = LoadPicture(vfPath & "nopic.gif")
       End If
       
       End With

  6. #6
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Loadpicture not working

    Highlight the line and press F9. This will make the macro break just before the line is executed and you can use the Immediate pane to print out what the expersssion evaluates to, i.e.,

    ?vfPath & "\" & lblVictimName.Caption & ".jpg" in the immediate pane will show you what the expression will evaluate to.

    I find the break very valuable in debugging to see what is going on. After veiwing in the immediate pane, press F5 for the macro to continue executing.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Loadpicture not working

    Have you tested with a file type other than .gif?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. LoadPicture() Issue
    By ykobure in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2015, 08:15 AM
  2. LoadPicture() Issue
    By ykobure in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2015, 10:48 AM
  3. Replies: 4
    Last Post: 09-03-2013, 02:52 AM
  4. LoadPicture from existing image/shape
    By mateoc15 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-07-2012, 05:16 AM
  5. Load picture into form not using 'LoadPicture'
    By pfrattali in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2011, 02:07 AM
  6. LoadPicture for a ControlButton help
    By micmacuk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-30-2006, 03:50 PM
  7. [SOLVED] conditional compile for LoadPicture?
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2006, 10:15 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1