Results 1 to 5 of 5

Thread: How to add image text automatically to images?

  1. #1

    How to add image text automatically to images?

    Hi All,

    I'm making an A1 poster with 346 images of people and I want to make a grid of images with the name of the person plus location (like "Robert James - London") in a text box under the image.

    I got as far as creating the image grid with blank text boxes.

    I have all the images in a folder (like 1.jpg, 2.jpg) and I have all the associated text in another folder (like 1.txt, 2.txt).

    I could add this manually but... phew... and it may change so being able to just re-run a program would be super helpful.

    If there was a way do this programmatically I would be grateful to know how

    I'm a bit of an old fart. I don't know VBA but can program in Python, Cobol and err.. 6502 assembler.

    I did say I was old!


    Wilson Logan.

  2. #2
    I don't have your exact use case; I helped a person before to add images to a table. I switched it around to also add the file name, which it seems you're doing? (or is the name contained inside the text file?)

    This code loops a folder for all the jpg files, put the image in a table and writes the name of the image file in the associated row

    Private Sub btnAddPictures_Click()
        Dim tbl As Table
        Set tbl = FindTable("imagesTable", Slide2)
        Dim currentTableRow As Integer
        currentTableRow = 1
        Dim shp As Shape
        Dim imageFileName As String ' add Microsoft Scripting Runtime (in tools > references)
        imageFileName = Dir("F:\PPT_SlideImages\*.jpg")
        Do While Len(imageFileName) > 0
            tbl.Cell(currentTableRow, 1).Shape.Fill.UserPicture "F:\PPT_SlideImages\" & imageFileName
            tbl.Cell(currentTableRow, 2).Shape.TextFrame.TextRange.Text = imageFileName
            imageFileName = Dir
            currentTableRow = currentTableRow + 1
    End Sub
    Private Function FindTable(tblName As String, whichSlide As Slide) As Table
        Dim shp As Shape
        For Each shp In whichSlide.Shapes
            If shp.HasTable And shp.Name = tblName Then
                ' this is an image and it has logo in the name
                Set FindTable = shp.Table
                Exit Function
            End If
    End Function
    Attached Images Attached Images

  3. #3

    My images aren't in a table but I guess I could do that and the description is in the filename but I can probably make the description the filename so.. yeah... I think this could work.

    Thanks again!


  4. #4
    You're welcome, happy to help a fellow grey beard COBOLian (also RPG II here)
    You definitely add code to open the associated text file to grab the name contained therein, if you need that.

    EDIT: create a function like this
    Add Microsoft Scripting Runtime is Tools > References, add code to read the text file.
    Private Function ReadNameFromFile(imageFileName as String) as String 
        ' open the text file that shares the name of the image file passed
        ' read the name in it and pass it back.
        Dim textFileName as String
        Dim txtFile as TextStream
        Dim txtName as String 
        ' remove the image extension add the txt
        textFileName = Left(imageFileName, Len(imageFileName - 4)) & ".txt"
        Set txtFile = fso.OpenTextFile(textFileName, ForReading)
        txtName = txtFile.ReadLine  ' there is just one line containing the name?
        Set txtFile = Nothing
        ReadNameFromFile = txtName
    End Function
    You'll want to add some error trapping for no file and empty file.

    edit2: fix typo and added removed blank lines when I edited it just before now
    Last edited by jdelano; 05-27-2024 at 03:56 AM.

  5. #5
    Yes, the text file is '<name> "Location -" <location>'

    e.g. "Robert Shaw Location - London"

    so swapping the line of txt for the filename should work.

Posting Permissions

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