Send Current Record Email Body

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Send Current Record Email Body

Post by D Willett »

Hi guys.
It's such a long time since I did anything like this and can't find any examples I have used before.

Send current record via email in the body ( not as attachment)

Here's my uncomplete code, could just do with the current record bit if possible:

Code: Select all

Private Sub cmdSendToEmail_Click()

Dim olApp As Object
Dim olMail As Object
Dim strBody As String

[b]'strBody = ??? NEED THIS TO ENTER THE CURRENT RECORD IN THE BODY, EACH FIELD ON A NEW LINE vbCrLf
'Table is "tblRework"
'Fields are [ReportDate], [JobID], [FaultReported], [WhoDealtWithThis], [ResolveOutcome][/b]

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.createitem(0)

With olMail
    .To = "dave.willett@someemail.com"
    .Subject = "Test Subject"
    .Body = strBody
    .Display
End With

Set olMail = Nothing
Set olApp = Nothing

End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Send Current Record Email Body

Post by HansV »

Hi Dave,

Do you just want the values of the fields, or do you want to add labels, for example something like:

Report Date: 12/06/2023
Job ID: 3876
...
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Send Current Record Email Body

Post by D Willett »

How does this look Hans ( Chat GPT )

Code: Select all

Private Sub Command13_Click()

    ' Create Outlook objects
    Dim olApp As Object
    Dim olMail As Object
    
    ' Create record-related variables (replace with your own variables or record retrieval logic)
    Dim recordID As Long
    Dim recordName As String
    Dim recordDescription As String
    Dim faultdealtwith
    Dim ResolveOutcome
    
    ' Set record values (replace with your own logic to retrieve the current record)
    recordID = Me![JobID]
    recordName = Me![ReportDate]
    recordDescription = Me![Fault Reported]
    faultdealtwith = Me![WhoDealtwithThis]
    ResolveOutcome = Me![ResolveOutcome]
    
    
    ' Create email body
    Dim emailBody As String
    emailBody = "JobID: " & recordID & vbCrLf & _
                "Date Reported: " & recordName & vbCrLf & _
                "Fault Reported: " & recordDescription & vbCrLf & _
                "Who Dealt With This: " & faultdealtwith & vbCrLf & _
                "How Was This Resolved: " & ResolveOutcome
                
                
    ' Create Outlook application and mail item
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
    
    ' Set email properties
    With olMail
    .To = "dave.willett@emailuk.com"
        .Subject = "Comeback Case" & " " & Me.JobID
        .Body = emailBody
        .Display ' Display the email
    End With
    
    ' Clean up Outlook objects
    Set olMail = Nothing
    Set olApp = Nothing

End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Send Current Record Email Body

Post by HansV »

That looks fine, but make sure that the field names are spelled exactly as on your form.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Send Current Record Email Body

Post by D Willett »

Yes done that Hans.
We are being taken over by AI :flee:
Cheers ...

Dave.