XL 2019 Mail en HTML vs VBA

gilles37

XLDnaute Occasionnel
Bonjour à tous,

J'ai un envoi mail en vba mais je souhaiterai qu'il soit en html afin d'améliorer la lecture.
Je n'y arrive pas.😕
Ci-dessous le code vba.( html sera compatible avec le reste des macros en VBA?)



VB:
Private Sub CommandButton3_Click()

Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xOutMsg As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
   
   
   
   
   '=============================================================
  
    'Création de l'objet Outlook
    Set ApplicOutlook = CreateObject("Outlook.Application")



    'Extraction des données
       
   '====================================
    Sujet = "rapport N°" & TextBox1.Value & " " & "Equipement" & " " & ComboBox11.Value & " " & ">>> Message automatique <<<"
   '====================================
       
    'Composition du message
   
   '===================================
    msg = "le " & "  " & TextBox16.Value & "  " & TextBox17.Value & "  " & TextBox18.Value & vbLf & vbLf   '[COLOR=rgb(65, 168, 95)]( avoir le "le" en gras souligné)[/COLOR]
   '===================================
       
    msg = msg & "Equipement : " & ComboBox11.Value & vbLf &  ' [COLOR=rgb(97, 189, 109)]( avoir  "Equipement " en gras souligné)[/COLOR]
   
   
    msg = msg & "commentaire / Analyse:    " & TextBox5.Value & vbLf & vbLf '[COLOR=rgb(65, 168, 95)]( avoir  "commentaire / Analyse " en gras souligné)[/COLOR]
   
    msg = msg & "Actions curatives:    " & TextBox6.Value & vbLf & ' [COLOR=rgb(65, 168, 95)]( avoir  "Actions curatives: " en gras souligné)[/COLOR]
   
   
    msg = msg & "Fiche établie par:   " & ComboBox8.Value & vbLf & vbLf [COLOR=rgb(65, 168, 95)]' ( avoir  "Fiche établie par: " en gras souligné)[/COLOR]
   
   
    msg = msg & "" & vbLf & vbLf
    msg = msg & "===================== Ne pas répondre, message automatique ==================================="
       
    'Création de l'élément de courrier et envoi
   ' Set ElémentCourrier = ApplicOutlook.CreateItem(0)
   
    '======================================================

 With xOutMail
 
     
           '.To = Range("Adres2").Value
          '.CC = Range("Adres1").Value
            .BCC = Range("Adres3").Value
            .Subject = Sujet
            .HTMLBody = msg
        .Display
 .Send
 ActiveWorkbook.SaveAs Filename:="X:\suivi des rapports équipements.xlsm", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    CommandButton1 = True

 
 
   
  .HTMLBody = xOutMsg
        .Display
    End With
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Merci a vous et bonnes fêtes de fin d'année
 
Dernière modification par un modérateur:
Solution
re,

intégrez un retour chariot à la place des vblf, juste cette balise <br/>
j'ai ajouté les mises en couleur, pas testé car pas de fichier de test fourni mais cela devrait fonctionner
J'ai aussi mis le message en police Arial taille 11.5, plus lisible à mon sens que la Calibri 10 par défaut.

Cordialement, @+
VB:
Private Sub CommandButton3_Click()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xOutMsg As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
'=============================================================
'Création de l'objet Outlook
Set ApplicOutlook = CreateObject("Outlook.Application")
'Extraction des données
'====================================
Sujet =...

Bernard_XLD

XLDnaute Barbatruc
Membre du Staff
re,

essayez comme cela, pas testé.
et, s'il vous plait, mettez vos codes dans les balises de code à l'aide du bouton </>, c'est beaucoup plus agréable pour ceux qui vous lisent

Cordialement, @+
VB:
Private Sub CommandButton3_Click()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xOutMsg As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
'=============================================================
'Création de l'objet Outlook
Set ApplicOutlook = CreateObject("Outlook.Application")
'Extraction des données
'====================================
Sujet = "rapport N°" & TextBox1.Value & " " & "Equipement" & " " & ComboBox11.Value & " " & ">>> Message automatique <<<"
'====================================
'Composition du message
'===================================
msg = "<u><b>le </b></u>" & " " & TextBox16.Value & " " & TextBox17.Value & " " & TextBox18.Value & vbLf & vbLf '( avoir le "le" en gras souligné)
'===================================
msg = msg & "<u><b>Equipement : </b></u>" & ComboBox11.Value & vbLf  ' ( avoir "Equipement " en gras souligné)
msg = msg & "<u><b>commentaire / Analyse: </b></u>" & TextBox5.Value & vbLf & vbLf '( avoir "commentaire / Analyse " en gras souligné)
msg = msg & "<u><b>Actions curatives: </b></u>" & TextBox6.Value & vbLf  ' ( avoir "Actions curatives: " en gras souligné)
msg = msg & "<u><b>Fiche établie par: </b></u>" & ComboBox8.Value & vbLf & vbLf ' ( avoir "Fiche établie par: " en gras souligné)
msg = msg & "" & vbLf & vbLf
msg = msg & "===================== Ne pas répondre, message automatique ==================================="

'Création de l'élément de courrier et envoi
' Set ElémentCourrier = ApplicOutlook.CreateItem(0)

'======================================================

With xOutMail

'.To = Range("Adres2").Value
'.CC = Range("Adres1").Value
.BCC = Range("Adres3").Value
.Subject = Sujet
.HTMLBody = msg
.Display
.Send
ActiveWorkbook.SaveAs Filename:="X:\suivi des rapports équipements.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
CommandButton1 = True

.HTMLBody = xOutMsg
.Display
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

gilles37

XLDnaute Occasionnel
Bonjour Bernard_XLD, le forum

Le résultat est au rendez vous sauf qu'il n'y a pas de sauf a la ligne

VB:
msg = msg & "<u><b>Equipement : </b></u>" & ComboBox11.Value & vbLf  ' [COLOR=rgb(41, 105, 176)]( souhaite retour a la ligne)[/COLOR]
msg = msg & "<u><b>commentaire / Analyse: </b></u>" & TextBox5.Value & vbLf & vbLf[COLOR=rgb(41, 105, 176)] '( souhaite retour a la ligne)[/COLOR]
msg = msg & "<u><b>Actions curatives: </b></u>" & TextBox6.Value & vbLf [COLOR=rgb(41, 105, 176)] ' ( souhaite retour a la ligne)[/COLOR]
msg = msg & "<u><b>Fiche établie par: </b></u>" & ComboBox8.Value & vbLf & vbLf [COLOR=rgb(41, 105, 176)]' ( souhaite retour a la ligne)[/COLOR]
msg = msg & "" & vbLf & vbLf
 
Dernière modification par un modérateur:

Bernard_XLD

XLDnaute Barbatruc
Membre du Staff
re,

intégrez un retour chariot à la place des vblf, juste cette balise <br/>
j'ai ajouté les mises en couleur, pas testé car pas de fichier de test fourni mais cela devrait fonctionner
J'ai aussi mis le message en police Arial taille 11.5, plus lisible à mon sens que la Calibri 10 par défaut.

Cordialement, @+
VB:
Private Sub CommandButton3_Click()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xOutMsg As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
'=============================================================
'Création de l'objet Outlook
Set ApplicOutlook = CreateObject("Outlook.Application")
'Extraction des données
'====================================
Sujet = "rapport N°" & TextBox1.Value & " Équipement" & " " & ComboBox11.Value & " " & ">>> Message automatique <<<"
'====================================
'Composition du message
'===================================
Msg = "<u><b><font style=""color:#41A85F"" >le</font></b></u>" & " " & TextBox16.Value & " " & TextBox17.Value & " " & TextBox18.Value & "<br/><br/>"
'===================================
Msg = Msg & "<u><b><font style=""color:#61BD6D"" >Équipement:</font></b></u>" & " " & ComboBox11.Value & "<br/>"
Msg = Msg & "<u><b><font style=""color:#41A85F"" >Commentaire / Analyse:</font></b></u>" & " " & TextBox5.Value & "<br/><br/>"
Msg = Msg & "<u><b><font style=""color:#41A85F"" >Actions curatives:</font></b></u>" & " " & TextBox6.Value & "<br/>"
Msg = Msg & "<u><b><font style=""color:#41A85F"" >Fiche établie par:</font></b></u>" & " " & ComboBox8.Value & "<br/><br/>"
Msg = Msg & "" & "<br/><br/>"
Msg = Msg & "===================== Ne pas répondre, message automatique ==================================="
Msg = "<font style=""font-family:Arial;font-size:11.5pt"">" & Msg & "</font>"

'Création de l'élément de courrier et envoi
' Set ElémentCourrier = ApplicOutlook.CreateItem(0)

'======================================================

With xOutMail

'.To = Range("Adres2").Value
'.CC = Range("Adres1").Value
.BCC = Range("Adres3").Value
.Subject = Sujet
.HTMLBody = Msg
.Display
.Send
ActiveWorkbook.SaveAs Filename:="X:\suivi des rapports équipements.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
CommandButton1 = True

.HTMLBody = xOutMsg
.Display
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
Bonjour à tous
1° les balises <FONT> ont leur propre attribut "color" donc pas besoins de passer par le (Style CSS)

2° afin de ne pas t'ennuyerla prochaine fois a chercher le code couleurhtml par rapport a une couleur excel (long/vb/rgb/etc..), je vous propose ma fonction de conversion

VB:
Private Sub CommandButton3_Click()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xOutMsg As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
'=============================================================
'Création de l'objet Outlook
Set ApplicOutlook = CreateObject("Outlook.Application")
'Extraction des données
'====================================
Sujet = "rapport N°" & TextBox1.Value & " Équipement" & " " & ComboBox11.Value & " " & ">>> Message automatique <<<"
'====================================
'Composition du message
'===================================
Msg = "<u><b><font color=" & coul_XL_to_coul_HTMLX(RGB(65, 168, 95)) & " >le</font></b></u>" & " " & TextBox16.Value & " " & TextBox17.Value & " " & TextBox18.Value & "<br/><br/>"
'===================================
Msg = Msg & "<u><b><font color=" & coul_XL_to_coul_HTMLX(RGB(97, 189, 109)) & " >Équipement:</font></b></u>" & " " & ComboBox11.Value & "<br/>"
Msg = Msg & "<u><b><font color=" & coul_XL_to_coul_HTMLX(RGB(65, 168, 95)) & "  >Commentaire / Analyse:</font></b></u>" & " " & TextBox5.Value & "<br/><br/>"
Msg = Msg & "<u><b><font color=" & coul_XL_to_coul_HTMLX(RGB(65, 168, 95)) & "  >Actions curatives:</font></b></u>" & " " & TextBox6.Value & "<br/>"
Msg = Msg & "<u><b><font color=" & coul_XL_to_coul_HTMLX(RGB(65, 168, 95)) & "  >Fiche établie par:</font></b></u>" & " " & ComboBox8.Value & "<br/><br/>"
Msg = Msg & "" & "<br/><br/>"
Msg = Msg & "===================== Ne pas répondre, message automatique ==================================="
Msg = "<font style=""font-family:Arial;font-size:11.5pt"">" & Msg & "</font>"

'Création de l'élément de courrier et envoi
' Set ElémentCourrier = ApplicOutlook.CreateItem(0)

'======================================================

With xOutMail

'.To = Range("Adres2").Value
'.CC = Range("Adres1").Value
.BCC = Range("Adres3").Value
.Subject = Sujet
.HTMLBody = Msg
.Display
.Send
ActiveWorkbook.SaveAs Filename:="X:\suivi des rapports équipements.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
CommandButton1 = True

.HTMLBody = xOutMsg
.Display
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub

Function coul_XL_to_coul_HTMLX(couleur)
'fonction HTMLCOLOR ---> By Patricktoulon
    Dim str0 As String, strf As String
    str0 = Right("000000" & Hex(couleur), 6): strf = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
    coul_XL_to_coul_HTMLX = "#" & strf & ""
End Function
 
Dernière édition:

Discussions similaires

Réponses
6
Affichages
294

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 105
Messages
2 085 350
Membres
102 870
dernier inscrit
Armisa