Hi all,
1st post (v exciting...) and I am creating a database containing info, including a *.jpg image, for each machine we have on offer.
The images are all successfully loaded and I can see them when linked to a form and I flick through the records.
My aim is to have a button on the form to run some VBA which creates an Outlook email object. I put the request into ChatGPT and got the code below, which is returning a "Run-time error 438, Object doesn't support this property or method" error on the line "If rs!Machine_Image.Attachments.Count > 0 Then" (in bold from sub contained below)
The *.jpg image is in attachment Data Type Field, Field Name "Machine_Image" in DataBase "Machines"
Full code is below, called directly from a command button on a form.
There are 50 rows in the database and I want to extract all data on the row currently being shown on the form.
I also tried using a DAO.Attachment data type but couldn't find the Microsoft Access Data Access Object Library (I think that was the name) to reference so that also failed.
Hope that explains everything? I expect this is a basic error but I'm used to VBA in Excel (don't hate me...) so diagnosing this will hopefully be simple but it's outside what is immediately familiar to me.
Happy for any solution (referencing the Library or using the attachment as Object below) - just want t make this work and very grateful for any help
Cheers
Nigel
Code:
Sub SendEmailWithImage()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim rs As DAO.Recordset
Dim attachmentPath As String
Dim strSQL As String
Dim HTMLBody As String
Dim tempFolder As String
Dim attachment As Object ' Use a generic object to reference the attachment
' Initialize Outlook Application
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0) ' 0 is for a new email
' Define the path to save the temporary image
tempFolder = "C:\Temp" ' Adjust this folder path if needed
' Get the image from the "machines" table (attachment field "Machine_Image")
strSQL = "SELECT Machine_Image FROM Machines WHERE ID = 3" ' Change "id = 1" as needed to get the correct record
Set rs = CurrentDb.OpenRecordset(strSQL)
If Not rs.EOF Then
' Loop through the attachment field to check if there are any attachments
If rs!Machine_Image.Attachments.Count > 0 Then
' Get the first attachment from the field
Set attachment = rs!Machine_Image.Attachments.Item(0) ' Get the first attachment
' Save the attachment to the temp folder
attachmentPath = tempFolder & "image.jpg" ' Specify the path to save the image
attachment.SaveToFile attachmentPath ' Save the image file
' Compose the email with HTML body
HTMLBody = "<html><body>"
HTMLBody = HTMLBody & "<p>Please find the image below:</p>"
HTMLBody = HTMLBody & "<img src='cid:image1'>" ' Reference the image using cid
HTMLBody = HTMLBody & "</body></html>"
' Compose the email
With OutlookMail
.Subject = "Subject of the Email"
.BodyFormat = 2 ' olFormatHTML
.HTMLBody = HTMLBody
.To = "recipient@example.com" ' Specify the recipient's email address
' Attach the image inline (CID)
.Attachments.Add attachmentPath, 1, 0, "image1" ' 1 means olByValue, "image1" is the CID used in the HTML
.Display ' Show the email before sending (you can use .Send to send automatically)
End With
Else
MsgBox "No image found in the attachment field.", vbExclamation
End If
Else
MsgBox "No record found for the selected machine.", vbExclamation
End If
' Clean up
rs.Close
Set rs = Nothing
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub