Send Image Attachment with Outlook (1 Viewer)

Kayleigh

Member
Local time
Today, 14:21
Joined
Sep 24, 2020
Messages
706
Hi
I currently have a continuous form which when edited will send the new text as an email to the relevant parties. I would like to add another field which would be an image attachment so the user can select a file to be sent with their text. What code would be required to achieve this?
 

conception_native_0123

Well-known member
Local time
Today, 09:21
Joined
Mar 13, 2021
Messages
1,826
That's pretty complicated. Look up on Google the attachments collection in VBA and the add method that's attached to that collection. That's where you start. I know this post doesn't really help that much, but there is a plethora of coding samples for VBA that does this exact thing if you look it up on Google.
 

Kayleigh

Member
Local time
Today, 14:21
Joined
Sep 24, 2020
Messages
706
I have the code to send the email through outlook application. Would that not be easier?
 

Kayleigh

Member
Local time
Today, 14:21
Joined
Sep 24, 2020
Messages
706
This is the code I currently use. What would the code be to add attachments?
Code:
Private Sub cmdSend(recipient As String)


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim txt As String
Dim varResponse As Variant

On Error GoTo Err_cmdSend

If IsNothing(Forms!frmOrderMan!frmOrderLog.Form!fldJLNote) Then
    varResponse = MsgBox("No text found. Would you like to add message?", vbYesNo, gtstrAppTitle)
    If varResponse = vbYes Then
        Exit Sub
    End If
End If

txt = Nz(Forms!frmOrderMan!frmOrderLog.Form!fldJLNote, "")

DoCmd.Hourglass True

Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim varWhere As Variant
Dim Subjectline As String
Dim strDearName As Variant
Dim signature As String
Dim mail1 As Variant
Dim mail2 As Variant
Dim strBody As String
Dim strClient As String
Dim strTSEmail As Variant
Dim strCompany As Variant
Dim strCompPhone As String
Dim strCompEmail As String
Dim strCompPhone1 As String
Dim strCompEmail1 As String
Dim strCompPhone2 As String
Dim strCompEmail2 As String
Dim rsemail As DAO.Recordset
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Dim mySQL As String
Dim varDblGlzRef As Variant
Dim strDescription As String
Dim varX As Variant
varX = Forms!frmlogin!cmbstaff

If Not IsFormLoaded("frmOrderMan") Then Exit Sub

DoCmd.SetWarnings False
Set MyOutlook = New Outlook.Application
Set MyOutlook = CreateObject("Outlook.Application")
Set Ns = MyOutlook.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
MyOutlook.Explorers.Add Folder

strTSEmail = ""
mail1 = ""

strDearName = ""

If Trim(Forms!frmOrderMan!fldDblGlzSysRef & "") = "" Then
varDblGlzRef = "No ref"
Else
varDblGlzRef = Nz(Forms!frmOrderMan!fldDblGlzSysRef)
End If

mail1 = recipient
'Debug.Print recipient

strClient = ""
Subjectline = "JOB LOG: [" & Nz(Forms!frmOrderMan!fldOrderID) & "] " & varDblGlzRef & " - " & Nz(Forms!frmOrderMan!fldOClientID.Column(1)) & " - " & Nz(Forms!frmOrderMan!fldOAddressID.Column(1)) & " - " & Nz(Forms!frmOrderMan!fldOJobDescription)
''debug.Print Subjectline
strBody = txt

Set MyMail = MyOutlook.CreateItem(olMailItem)

 
MyMail.Recipients.Add mail1


MyMail.Subject = Subjectline
MyMail.Body = strBody
If varX = 14 Or IsNothing(varX) Then
    MyMail.Display
Else
    MyMail.Send
End If
'SendKeys "^{END}", True 'moves curser to the end
'SendKeys "{End}", True
'SendKeys "%nas{enter}", True 'to add default signature

'If GetNumlock() Then
'  MsgBox "Num Lock is ON!"
'Else
 
'  SendKeys "{NUMLOCK}"
'End If

Set MyMail = Nothing
Set MyOutlook = Nothing
DoCmd.SetWarnings True
DoCmd.Hourglass False
Dim varZ As Variant
varZ = MsgBox("Message sent successfully", vbInformation + vbOKOnly, gtstrAppTitle)

Exit_cmdSend:
    Exit Sub

Err_cmdSend:
    MsgBox Err.Description, vbExclamation, "cmdSend Error " & Err.Number
    Resume Exit_cmdSend

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:21
Joined
May 7, 2009
Messages
19,175
add this line:
Code:
        'you can have multiple attachment, just put in Array
        'or a single attachment.
        'attachment should include the "complete" path to the file.
        Dim AttachmentFile As Variant
        Dim varItem As Variant
        'AttachmentFile = Array("D:\folder\note.pdf", "d:\image\mypic.png")
        AttachmentFile = "D:\image\mypic.png"
        With MyMail
           If IsArray(AttachmentFile) Then
               For Each varItem In AttachmentFile
                   .Attachments.Add varItem
               Next
           ElseIf IsNull(AttachmentFile) = False Then
            .   Attachments.Add AttachmentFile
           End If
        End With
 

Users who are viewing this thread

Top Bottom