Copy image file(.jpg) from a folder RRS feed

  • Question

  • Hi,

    I have some image files stored in a particular folder. I want to copy all the images from that folder and paste those onto a word file. Then system should save the word file with the name of the folder from which i have selected the image files.

    For Example: I have one folder in C drive called 'Images'. Now under 'Images' folder I have multiple subfolder like Image1,image2 ,Image3 and so on.. Under Image1 folder I have some number of image files. Same way Image2 folder has some image files in it.Same way Image3. Once I click on a button from my excel sheet system should locate C:\Images folder and copy all the images from all Image1,Image2,Image3 one by one and save those docx file in D drive.

    The o/p result will be like

    Image1.docx--All the image files will be copied from image1 folder and paste in Image1.docx file

    Image2.docx--All the image files will be copied from image2 folder and paste in Image2.docx file

    Image3.docx--All the image files will be copied from image3 folder and paste in Image3.docx file


    • Edited by JO_LO Thursday, April 30, 2015 1:57 AM
    Wednesday, April 29, 2015 2:37 PM


  • Hi JO_LO,

    To iterate the files in a folder include subfolders, please refer to this code below:

    Sub LoopTest()
    Dim FileSystem As Object
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    Dim f As Folder
    HostFolder = "D:\Files\"
    DoFolder FileSystem.GetFolder(HostFolder)
    End Sub
    Sub DoFolder(Folder)
        Dim SubFolder
        For Each SubFolder In Folder.SubFolders
            DoFolder SubFolder
        Dim File
        For Each File In Folder.Files
            ' Operate on each file
            Debug.Print File
    End Sub

    To insert image to word, we could use InlineShapes.AddPicture method.

    To save the word document to a folder, we could use Workbook.SaveAs method.

    On the other hand, we also need to add word object library reference if we want to operate word in the excel. (Tools=>References)



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 30, 2015 5:54 AM