简体   繁体   中英

Include cell value in email body - code fix

My code is sending an e-mail through outlook.
I want the value of two cells included in the e-mail body.
Formatting of the e-mail is working fine and the text is properly shown. Also the cell value is shown properly in one of two cases. For one part of the code, it is adding ACCT:ACCT:ACCT: in front of the cell value. Any help on how I can get this fixed?

The cell value for cell (6,3) = Zeer Dringend
The cell value for cell (22,3) = 2019-0004

This is the e-mail body generated by the code

Beste Collega,

Een nieuwe retour zending registratie werd aangemaakt met urgentie: ACCT:ACCT:ACCT:Zeer Dringend .

Het pakket nummer is 2019-0004 .

In geval van vragen gelieve contact op te nemen.

Met vriendelijke groeten,

So where it shows ACCT:ACCT:ACCT:Zeer Dringend , it should say Zeer Dringend

This is the full code

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim WS As Worksheet

If ActiveWorkbook.Path <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set WS = Sheets("Ingave")

strbody = "<font size=""3"" face=""Calibri"">" & _
"Beste Collega,<br><br>" & _
"Een nieuwe retour zending registratie werd aangemaakt met urgentie: <B>" & WS.Cells(6, 3).Value & "</B>.<br>" & _
"Het pakket nummer is <B>" & WS.Cells(22, 3).Value & "</B>.<br><B> " & _
"</B><br><br>In geval van vragen gelieve contact op te nemen." & _
"<br><br> Met vriendelijke groeten, </font>"
On Error Resume Next

With OutMail
.to = "xxx@yyy"
.CC = ""
.BCC = ""
.Subject = "Nieuwe registratie retour pakket "
.HTMLBody = strbody
.Display   'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "U moet de file eerst opslaan voor u verder kan gaan."
End If

The code you provided seems to work as expected, you should attempt to investigate where the ACCT:ACCT:ACCT: string is actually coming from, maybe the following helps, where I've changed the ActiveWorkbook to ThisWorkbook to eliminate the possibility of the code picking up data from a workbook that might be active, but not necessarily the intended one:

Sub Send_Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Ingave")
'make sure that we are looking at the this workbook and not the Active one.

If ThisWorkbook.Path <> "" Then
'make sure the workbook has been saved, so we get a Path property.

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "<font size=""3"" face=""Calibri"">" & _
    "Beste Collega,<br><br>" & _
    "Een nieuwe retour zending registratie werd aangemaakt met urgentie: <B>" & ws.Cells(6, 3).Value & "</B>.<br>" & _
    "Het pakket nummer is <B>" & ws.Cells(22, 3).Value & "</B>.<br><B> " & _
    "</B><br><br>In geval van vragen gelieve contact op te nemen." & _
    "<br><br> Met vriendelijke groeten, </font>"
    'On Error Resume Next

    With OutMail
        .to = "xxx@yyy"
        .CC = ""
        .BCC = ""
        .Subject = "Nieuwe registratie retour pakket "
        .HTMLBody = strbody
        .Display   'or use .Send
    End With
    'On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
Else
    MsgBox "U moet de file eerst opslaan voor u verder kan gaan."
End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM