2017-10-31 3 views
0

Ich habe useform, die E-Mails automatisch sendet. Ich möchte den Hauptteil der E-Mail ändern - einige davon werden auf einer Zelle mit Text basieren, so dass es dynamisch sein kann und einige im Code behoben werden. für jetzt - in läuft mir ein fehler von objekt erforderlich, ich werde dankbar sein für hilfe. Ich möchte, dass jede Zeile im Text der E-Mail getrennt ist.VBA Excel Outlook E-Mail Körper Formatierung

Sub sendMail(ByVal mail As String, name As String, Msht As Worksheet, CCmail As Integer, CCperson As String) 
    Dim applOL As Outlook.Application 
    Dim miOL As Outlook.MailItem 
    Dim recptOL As Outlook.Recipient 
    mailSub = Msht.Range("J2") 
    mailbody = Msht.Range("L2") 
    Set applOL = New Outlook.Application 
    Set miOL = applOL.CreateItem(olMailItem) 
    Set recptOL = miOL.Recipients.add(mail) 
    recptOL.Type = olTo 
    If CCmail = 1 Then 
     Set recptOL = miOL.Recipients.add(CCperson) 
     recptOL.Type = olCC 
    End If 
    tempPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.name 
    With miOL 
     .Subject = mailSub 
     .Body = "bla bla" & "bla bla bla" & mailbody.Font.Underline & Msht.Range("M2").Font.Bold & Body = Msht.Range("N2") 
     .Attachments.add (tempPath) 
     .send 

    End With 
    ActiveWorkbook.Close Savechanges:=True 
    Set applOL = Nothing 
    Set miOL = Nothing 
    Set recptOL = Nothing 
End Sub 

Antwort

1

Sie müssen HTML-Format an den Körper der E-Mail bewerben:

Dim body_ As String 
    body_= "<p> Hello </p>" & _ 
      "<p> This is a line </p>" & _ 
      "<p> This is another line </p>" & _ 
      "<p> This is yet another line. </p>" 

.BodyFormat = olFormatHTML 
.HTMLBody = "<html><head></head><body>" & body_ & "</body></html>" 

aktualisieren

Option Explicit 

Public Sub sendMail(ByVal mail As String, name As String, Msht As Worksheet, CCmail As Integer, CCperson As String) 
    On Error GoTo ErrorTrap 

    Dim applOL As Outlook.Application 
    Set applOL = New Outlook.Application 

    Dim miOL As Outlook.MailItem 
    Set miOL = applOL.CreateItem(olMailItem) 

    Dim recptOL As Outlook.Recipient 
    Set recptOL = miOL.Recipients.Add(mail) 
     recptOL.Type = olTo 

    Dim mailSub As String 
     mailSub = Msht.Range("J2") 

    Dim mailbody As String 
     mailbody = "<p><u>" & Msht.Range("L2").Value & "</u></p>" & _ 
        "<p><b>" & Msht.Range("M2").Value & "</b></p>" & _ 
        "<p>" & Msht.Range("N2").Value & "</p>" 

    If CCmail = 1 Then 
     Set recptOL = miOL.Recipients.Add(CCperson) 
     recptOL.Type = olCC 
    End If 

    Dim tempPath As String 
     tempPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.name 

    With miOL 
     .Subject = mailSub 
     .BodyFormat = olFormatHTML 
     .HTMLBody = "<html><head></head><body>" & mailbody & "</body></html>" 
     .Attachments.Add tempPath 
     .send 
    End With 

    ActiveWorkbook.Close Savechanges:=True 

Leave: 
    On Error GoTo 0 
    Exit Sub 

ErrorTrap: 
    MsgBox Err.Description, vbCritical 
    Resume Leave 
End Sub 
+0

wow danke, aber wenn ich einige wollen fett sein und einige beziehen zu spezifischem Text in Zellen. –

+1

Für die Fettschrift müssen Sie das HTML-Fett-Tag 'Text' verwenden. Für den Zellenwert wird eine einfache Kettenverkettung verwendet, z. '" Das ist "& .Cells (1,1) .Wert.' –

+0

es funktioniert nicht für mich, wenn ich es ändere, was ich brauche, kannst du es bitte für mich schreiben, wie es sein sollte? –

Verwandte Themen