Consulting

Results 1 to 10 of 10

Thread: A Class Module to store Images from a folder for later reuse in Workbook Excel VBA

  1. #1

    A Class Module to store Images from a folder for later reuse in Workbook Excel VBA

    I am importing csv data and images from a folder into a temporary Excel workbook, which is used for readying the data for reporting purposes and resizing the data-related images. I then plan to import this data and corresponding images into my current workbook.


    With ADO, i am able to import the csv data from the temporary workbook into my current workbook, BUT NOT THEIR CORRESPONDING IMAGES to generate different reports.


    I came to know that a Custom Class Module can store objects for later use. My question is, is there a way to store the pictures directly from the folder into a Class Module by the picture name (instead of inserting them into the temporary workbook), for later use for generating reports in current workbook?


    e.g. a Class Module to hold the pictures till the current workbook is open (maintain active session).


    Is this possible?

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    655
    Location
    Seems similar to your previous query except that U now want to use a temporary wb to store the images. You can load images into a collection for later use however the collection will only exist as long as the temporary wb is open. HTH. Dave

  3. #3
    Hi @Dave,

    If i plan to do away with the temp.xlsx workbook and just load the csvs and images into a Class Module in my Current workbook, so that they can remain in memory till the current workbook is open, how feasible is this to do?

  4. #4
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    655
    Location
    This is confusing reading both threads on the same topic. I'm not sure why U need a temporary wb?
    As I mentioned, U can load images into a collection that will persist as long as the wb is open.
    To illustrate, create a userform and add an image control and 3 command buttons. Add the following
    code to the userform....
    Private Sub CommandButton1_Click()
    'adjust file path to suit
    UserForm1.Image1.Picture = LoadPicture("C:\TestFolder\SomePic.gif")
    End Sub
    
    Private Sub CommandButton2_Click()
    Set PicCollect = New Collection
    PicCollect.Add UserForm1.Image1.Picture
    End Sub
    Private Sub CommandButton3_Click()
    With UserForm1.Image1
    .Picture = PicCollect(1)
    End With
    End Sub
    Add a module and insert this line of code...
    Public PicCollect As Collection
    To trial change the picture path to a valid picture file path. Show the userform.
    Select command1 to load the picture in the image. Select command2 to load the picture to the collection.
    Unload the userform. Show the userform. Select command3 to load the image from the collection.
    U could loop through all of your pics and load them into the collection and retrieve them as needed.
    HTH. Dave

  5. #5
    Sorry for the confusion @Dave. Just working on a weekend trying to close this issue has made me hanky-panky!

    You mean, do i need to add a Userform inorder to load each image to an image control to store them in my current workbook (if i go with your suggestion of doing away with the temporary workbook)?

    Is there a way to avoid the userform but create an image control instance for each image, store it in a collection and then use it as needed while current workbook is open?

  6. #6
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    655
    Location
    You will need some method of creating the images to store in the collection. The pic file
    address is just a string. You probably could use a chart or a frame control to load the
    image into. U only need to show the userform/chart/frame once and load the collection.
    Sticking with the previous example, change command2 to the following code
    (Adjust your folder path of images and file extension to suit)....
    Private Sub CommandButton2_Click()
    Dim FSO As Object, FolDir As Object, FileNm As Object
    Set PicCollect = New Collection
    Set FSO = CreateObject("scripting.filesystemobject")
    'change folder to suit
    Set FolDir = FSO.GetFolder("C:\TestFolder")
    For Each FileNm In FolDir.Files
    If FileNm.Name Like "*.gif" Then 'change file extension to suit
    UserForm1.Image1.Picture = LoadPicture("C:\TestFolder\" & FileNm.Name)
    PicCollect.Add UserForm1.Image1.Picture
    End If
    Next FileNm
    Set FSO = Nothing
    Set FolDir = Nothing
    End Sub
    Add a spin control to the userform. Add this code to the userform...
    Dim spincnt As Integer
    Private Sub SpinButton1_SpinDown()
    spincnt = spincnt - 1
    If spincnt < 1 Then
    spincnt = 1
    End If
    With UserForm1.Image1
    .Picture = PicCollect(spincnt)
    End With
    End Sub
    
    Private Sub SpinButton1_SpinUp()
    spincnt = spincnt + 1
    If spincnt > PicCollect.Count Then
    spincnt = PicCollect.Count
    End If
    With UserForm1.Image1
    .Picture = PicCollect(spincnt)
    End With
    End Sub
    To operate, select command2. The spin button will then loop through the pics.
    Unload the userform. Show the userform. The spin button will continue to show
    the pics in the collection. Not sure if this helps but the pics are converted to memory. Dave

  7. #7
    Thanks for the example @Dave. Though it doesn't help with my situation, it does answer one question that i had in mind, i.e. Pictures cannot be imported and stored directly to a collection or dictionary without loading them to some OLE control without which they may not be imported to workbook later. Also, it means Ole controls loaded with pictures cannot be saved to a Dictionary or Collection without a Userform to hold them.

    Appreciate your help here.

  8. #8
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    655
    Location
    You are welcome. Yes U need some container to first generate the images to store them in the collection after that they are available from the collection to do whatever U want just like any picture file (ie. U don't need the userform to access them). Dave

  9. #9
    You mean if the container is the Image Control, can it be stored in a Dictionary/Collection without the need of a userform?

  10. #10
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    655
    Location
    No. U initially need some container to generate the images. After U generate the images and the images are stored in a collection, U no longer need the container for anything. Dave

Tags for this Thread

Posting Permissions

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