+ Reply to Thread
Results 1 to 15 of 15

Search Image from a Folder

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    18

    Search Image from a Folder

    Hi to everybody, im a total novice in Excel programming.

    I really need a macro that from a targeted folder, if i write an image filename in a cell box, that image is showed in a Image control box.
    (Images are all in .jpg)

    Somebody can help me please?


    Thank you in advance for your help.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search Image from a Folder

    Hello AiKuchi,

    This macro will take the file name from cell "A1" on "Sheet1" and display in it in a Control Toolbox Image control on "Sheet1". The Image control will size itself to match the picture. If you need to change the worksheet name, cell, or name of the control, they are marked in red.
    Sub ShowPicFile()
    
      Dim Img As MSForms.Image
      Dim Pic As StdPicture
      Dim PicFile As String
      Dim PicPath As String
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet1")
        PicPath = "C:\Documents and Settings\Owner\My Documents\"
        
          PicFile = PicPath & Range("A1")
          Set Pic = LoadPicture(PicFile)
        
          Set Img = Wks.OLEObjects("Image1").Object
          With Img
            .AutoSize = True
            .Picture = Pic
            .PictureAlignment = fmPictureAlignmentCenter
          End With
        
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-02-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Search Image from a Folder

    Quote Originally Posted by Leith Ross View Post
    Hello AiKuchi,

    This macro will take the file name from cell "A1" on "Sheet1" and display in it in a Control Toolbox Image control on "Sheet1". The Image control will size itself to match the picture. If you need to change the worksheet name, cell, or name of the control, they are marked in red.
    Sub ShowPicFile()
    
      Dim Img As MSForms.Image
      Dim Pic As StdPicture
      Dim PicFile As String
      Dim PicPath As String
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet1")
        PicPath = "C:\Documents and Settings\Owner\My Documents\"
        
          PicFile = PicPath & Range("A1")
          Set Pic = LoadPicture(PicFile)
        
          Set Img = Wks.OLEObjects("Image1").Object
          With Img
            .AutoSize = True
            .Picture = Pic
            .PictureAlignment = fmPictureAlignmentCenter
          End With
        
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

    Thank you so much for your help Leith.

    I placed a Image control box on the sheet1 and then I followed your precise instructions and when i try to run the macro, i have this debug problem in the line:


     Set Pic = LoadPicture(PicFile)

    Runtime error: 76:

    Path not found



    Thank you again.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search Image from a Folder

    Hello AiKuchi,

    The error is because Excel can't find the file. Check the spelling of your file path (folder) and the file name.

  5. #5
    Registered User
    Join Date
    08-02-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Search Image from a Folder

    Quote Originally Posted by Leith Ross View Post
    Hello AiKuchi,

    The error is because Excel can't find the file. Check the spelling of your file path (folder) and the file name.

    Hello Leith,

    I checked the path (C:\images\) but it's right.

    The error is: File not found.

    in the same line
       Set Pic = LoadPicture(PicFile)
    But inside the folder there are many image files (.jpg)
    For example 01.jpg

    Thank you.
    Last edited by Aikuchi; 08-02-2009 at 08:06 AM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search Image from a Folder

    Hello AiKuchi,

    The file name must be typed in cell "A1" before you run the macro. You said...
    [quote]
    I really need a macro that from a targeted folder, if i write an image filename in a cell box, that image is showed in a Image control box.
    [/code]
    I understood "cell box" to be a cell, like "A1", on the on the worksheet.

    Chigai de gozaimasu ka?

  7. #7
    Registered User
    Join Date
    04-21-2015
    Location
    Köln,Germany
    MS-Off Ver
    2010
    Posts
    40

    Re: Search Image from a Folder

    Hello,

    I have a userform with a textbox field, a search command button, and an image. I am trying to find code to so that I can search for a file name (the picture name should be written in textbox) from a file on the computer of an image that will then cause that image to come up in the image once the command button is clicked. I am a novice at VBA/Macro. The images are all located on the C drive. Any help would be greatly appreciated.

    Thanks,

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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