Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    Create a list of images for email

    Hi Guy's, i need a little help on this one please ?

    I have spent a while trying different things to make this work!!!!

    I am looping files to go to email, the result of the list is correct in the Imm window (5 files in the folder)

    I dont' think i can generate a loop with in the email html body ?

    I can very easily open 5 emails by moving the loop to after email but my target is to have 1 x email with all 5 images on

    ie:
    table & image1
    <br>
    table & image2
    <br>
    table & image3
    etc etc....

    on this occasion the 5 images in the folder

    Once email generated, the files will get moved to another folder so there isn't an overload of images to open/send

    Code:
    sPath = "T:\DMT Ltd\Cam Images\"sFiles = Dir(sPath & "*.jpg")
    
    
    Do While Len(sFiles) > 0
            i = i + 1
            
            sFileList = sFiles
            sFiles = Dir()
    
    
            
            strEmailImages = sFileList
            
            Debug.Print sPath & sFileList
            'Imm Window showing 5 files
    Loop
        
        
        Set myItem = myApp.CreateItem(olMailItem)
        Set OutAccount = myApp.Session.Accounts.Item(1)
        With myItem
        .To = ""
        .subject = "Images"
        .HTMLBody = Replace(TOD, "|", "<br>") & "<br>" & "<br>" & _
        "Please find images we have taken, " & "<br>" & "<br>" & _
        strEmailImages & "<br>" & "<br>" & _
        strTableStart & "<br>" & _
        "<P><IMG border=2 hspace=0 alt='' src='file:" & sPath & sFileList & "' align=baseline></P>" & "<br>" & strTableEnd & _
        KR & "<br>" & "<br>" & _
        FName & "<br>" & "<br>" & _
        "<P><IMG border=0 hspace=0 alt='' src='file://T:/DMT Ltd/Logo Media/" & SigFile & "' align=baseline></P>" & "<br>" & "<br>" & _
        "<FONT color=#00008B>" & eDisc & "<br>" & "<FONT color =#00008B>" & eDisc2
        .SendUsingAccount = OutAccount
        .Display
        End With


  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    I dont' think i can generate a loop with in the email html body ?
    Why not. I did just that to add records to a html table when I used to send emails.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    WGM thank you, so I should add a loop inside the HTML

    start loop just before tablestart and end the loop after table end ?

    i will try that

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    I have tried adding a for loop within the file count to see if i can get the number of tables and images onto the mail body!

    it all adds up as there is x 5 images in the folder but struggling to loop them onto the mail body!!!

    Code:
    sPath = "T:\DMT Ltd\Cam Images\"sFiles = Dir(sPath & "*.jpg")
    
    
    Set fso = CreateObject("Scripting.FileSystemObject")
       
        Set objFiles = fso.GetFolder(sPath).Files
       
        intFiles = objFiles.Count - 1
       
        MsgBox intFiles     'Total number of files
    
    
    Do While Len(sFiles) > 0
            i = i + 1
            
            sFileList = sFiles
            sFiles = Dir()
            
            For x = 1 To intFiles
            
            strTableStart = "<table style='text-align:left;border:3px solid blue;font-family:calibri;border-collapse:collapse;padding:25px'><tr style='background:white;mso-highlight:blue'>"
            
                strEmailImages = sPath & sFileList
            
            strTableEnd = "</tr></table>"
            
            Next x
            
            Debug.Print sPath & sFileList
            'Imm Window showing 5 files
    Loop
        
        ' ORHGINAL FILE SOURCE "<P><IMG border=2 hspace=0 alt='' src='file:" & sPath & sFileList & "' align=baseline></P>"
        
        Set myItem = myApp.CreateItem(olMailItem)
        Set OutAccount = myApp.Session.Accounts.Item(1)
        With myItem
        .To = ""
        .subject = "Images"
        .HTMLBody = Replace(TOD, "|", "<br>") & "<br>" & "<br>" & _
        "Please find images we have taken, " & "<br>" & "<br>" & _
        strEmailImages & "<br>" & "<br>" & _
        strTableStart & "<br>" & _
        "<P><IMG border=2 hspace=0 alt='' src='file:" & sPath & strEmailImages & "' align=baseline></P>" & "<br>" & strTableEnd & _
        KR & "<br>" & "<br>" & _
        FName & "<br>" & "<br>" & _
        "<P><IMG border=0 hspace=0 alt='' src='file://T:/DMT Ltd/Logo Media/" & SigFile & "' align=baseline></P>" & "<br>" & "<br>" & _
        "<FONT color=#00008B>" & eDisc & "<br>" & "<FONT color =#00008B>" & eDisc2
        .SendUsingAccount = OutAccount
        .Display
        End With

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    I have not played around much with HTML, but surely there should only be one <table >and </table>

    Think about what you are trying to do.

    Get it working for 1 file, then proceed with the loop. You can loop 1 to 1 you know?

    Attached is how I did mine. I have given you the whole code, so you will need to pick out what you need, but just inspect it. There are a few comments in there.
    Attached Files Attached Files
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    Dave, to reinforce what WGM is saying - and if you don't mind me saying you do seem to struggle with loops in general so, spilt the task into big logical steps first.
    Ignore the detail and how, just outline the process. On paper if necessary, and follow it through.

    So, you want to email a number of images into one email.
    Big points are (and in this order):


    1. Open a New Email
    2. Create the Email header and one of detail (Subject etc)
    3. Create a Table
    4. Loop to Insert Table rows
    5. Finish the Loop
    6. Create the Email Ending
    7. Send the email


    It's simple really, If you map out your current process in the same way you will see the flaw in the middle section points 3 & 4.


    1. Open a New Email
    2. Create the Email header and one of detail (Subject etc)
    3. Loop to Insert rows
    4. Create a Table
    5. Finish the loop
    6. Create the Email Ending
    7. Send the email


    Does that help?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks Guy's i can get it going for 1 file but can't seem the get the array of files will read through what you have both suggested

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Not clear to me what you want. You want each image in its own table? Why? Is there other data? Don't need HTML table structure just to display images in email.

    Why do you concatenate strEmailImages for .HTMLBody twice?

    Perhaps you could do a mockup in Excel of how you want this email to appear.

    As advised, work it out in steps. Start with basic and expand. Get a simple loop working first. Consider:
    Code:
    Dim strPath As String, strFile As String, strEmailImages As String
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    
    Set appOutLook = New Outlook.Application
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    
    strPath = "T:\DMT Ltd\Cam Images\"
    strFile = Dir(strPath & "*.jpg")
    Do While strFile <> ""
        strEmailImages = strEmailImages & "<IMG src= " & strPath & strFile & " width=100 height=100><br>"
        strFile = Dir
    Loop
    
    With MailOutLook
        .To = "some address"
        .Subject = "Some Subject"
        .HTMLBody = "Please find images we have taken:<br>" & strEmailImages
        .Display
    End With
    Modifed to use table and row structure:
    Code:
    strTableStart = "<table style='text-align:left;border:3px solid blue;font-family:calibri;border-collapse:collapse;padding:5px'>"
    strIntro = "<tr style='background:white;mso-highlight:blue'>Please find images we have taken: </tr>"
    strTableEnd = "</table>"
    
    With MailOutLook
        .To = "some address"
        .Subject = "Some Subject"
        .HTMLBody = strTableStart & strIntro & strTableEnd & strEmailImages
        .Display
    End With
    Alternative is to zip images to Windows Compression folder and attach to email.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thank you, yes it is to have each image in it's own table and yes to insert the images into 1 email

    I can see its mainly the procedure where i am going wrong and where the loop is initiated

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I just noticed title of thread says "list of images" but your code is actually to embed images in email. Which do you really want?

    I don't understand why each image needs to be in separate table. But if you must:
    Code:
    strTableStart = "<table style='text-align:left;border:3px solid blue;font-family:calibri;border-collapse:collapse;padding:5px'>"
    
    strPath = "T:\DMT Ltd\Cam Images\"
    strFile = Dir(strPath & "*.jpg")
    Do While strFile <> ""
        strEmailImages = strEmailImages & strTableStart & "<tr><IMG src= " & strPath & strFile & " width=100 height=100></tr></table>"
        strFile = Dir
    Loop
    
    strIntro = strTableStart & "<tr style='background:white;mso-highlight:blue'>Please find images we have taken:</tr></table>"
    
    With MailOutLook
        .To = "some address"
        .Subject = "Some Subject"
        .HTMLBody = strIntro & strEmailImages
        .Display
    End With
    Last edited by June7; 01-09-2023 at 03:06 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi June7, thank you, this is my modified version, i am just trying to have each file with it's own filename excluding .jpg within the table

    Nearly there with this now thank you

    Code:
    sPath = "T:\DMT Ltd\Cam Images\"sFiles = Dir(sPath & "*.jpg")
    
    
    
    
        Set myItem = myapp.CreateItem(olMailItem)
        Set OutAccount = myapp.Session.Accounts.Item(1)
        With myItem
    
    
    Do While sFiles <> ""
        
        strList = strList & sFiles & ";" ' to get file names
        
            Me.lstFiles.RowSource = strList 'populate list
            
            For i = 1 To Me.lstFiles.ListCount - 1
            
                strFileNames = strFileNames & Me.lstFiles.Column(0, i)
                
            Next
            
        strEmailImages = strEmailImages & _
            "<table style='text-align:left;border:3px solid blue;font-family:calibri;border-collapse:collapse;padding:5px'><br>" & _
                Replace(strFileNames, ".jpg", "") & _
                "<P><IMG border=2 hspace=0 alt='' src='file:" & sPath & sFiles & "' align=baseline></P>" & "<br>" & _
            "</table><br>"
        
        sFiles = Dir
        
    Loop
    
    
    strIntro = "Please find images we have taken:"
    
    
        .To = "some address"
        .subject = "Some Subject"
        .HTMLBody = strTableStart & strIntro & strTableEnd & strEmailImages
        .Display
    End With

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    And what happens when you run that? I doubt what you want. I think you just moved farther away from your goal.

    The filename without .jpg can be obtained from the filename of file referenced in folder. So why is a listbox now involved?

    You need to clarify exactly what you are trying to accomplish.

    Again, I suggest you provide a mockup of what this output should look like.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi June7, i tried adding a list box to retrieve the file names then each table will have a file name and image

    I didn't know i can retrieve file names without a listbox ? it's the only method I have ever used in the past so I was using a method i know!

    If that can be don without the need for listing the file names, i would do it just to shorten and tidy up

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    All works to what the initial target was though, to add the images to the mail body, thanks to your suggestions

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    You are retrieving file names with code that loops through folder. Just truncate. If you know every file will have .jpg extension, it's simple.
    Code:
    Do While strFile <> ""
        strEmailImages = strEmailImages & Replace(strFile, ".jpg","") & strTableStart & "<tr><IMG src= " & strPath & strFile & " width=100 height=100></tr></table>"
        strFile = Dir
    Loop
    This does assume nowhere else in file name is substring ".jpg".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 01-01-2021, 08:50 AM
  2. Replies: 4
    Last Post: 04-10-2019, 06:15 PM
  3. Replies: 22
    Last Post: 05-14-2018, 05:24 PM
  4. Replies: 5
    Last Post: 05-07-2014, 09:25 AM
  5. Replies: 2
    Last Post: 02-04-2010, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums