VBA Envoi par mail fichier Excel en PJ et d'une image dans corps de mail

guil660

XLDnaute Nouveau
Bonjour à tous,

Je cherche un moyen d'envoyer à partir de mon fichier Excel par mail mon fichier en PJ et de copier / coller comme une image une selection de cellules dans le corps de mon mail, tout en pouvant ecrire un texte au dessus et au dessous de l'image.

J'utilisais jusqu'à aujourd'hui la macro ci-dessous mais je ne sais pas comment insérer l'image au milieu de mmon texte :

Sub EnvoiMailAuDR()

Dim OutlookApp As New Outlook.Application
Dim NewMail As Outlook.MailItem

Set NewMail = OutlookApp.CreateItem(olMailItem)

'Liste des destinataires
With NewMail
.Recipients.Add (Sheets("FICHIERS TXT").Range("Y1").Value)

'Sujet :
.Subject = "Detail - " & " " & Sheets("FICHIERS TXT ").Range("X1").Value
'Corps du mail :
.Body = "Bonjour," & vbCrLf _
& vbCrLf _
& "Vous trouverez ci joint le détail" & vbCrLf _
& vbCrLf _
& "Cordialement" & vbCrLf _
& vbCrLf & vbCrLf _
& "GUI"

.Attachments.Add ActiveWorkbook.FullName
.Display
'Envoi :
.Send
End With
End Sub

Merci à vous par avance
 

Yaloo

XLDnaute Barbatruc
Re : VBA Envoi par mail fichier Excel en PJ et d'une image dans corps de mail

Bonjour guil660,

Voici une fonction de l'excellent Ron De Bruin :

VB:
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
 
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
 
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
 
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
 
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
 
    'Close TempWB
    TempWB.Close savechanges:=False
 
    'Delete the htm file we used in this function
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

A utiliser comme ça, par exemple :

VB:
Sub EnvoiMailAuDR()

 Dim OutlookApp As New Outlook.Application
 Dim NewMail As Outlook.MailItem
 Dim Debut$, Fin$
 Set NewMail = OutlookApp.CreateItem(olMailItem)

 'Mettre ici ta plage à insérer dans ton corps de mail
 Set rng = Sheets("MailRangeSelection").Range("B19:B24").SpecialCells(xlCellTypeVisible)

 Debut = "Bonjour, <BR><BR><BR>Vous trouverez ci joint le détail <BR>"
 Fin = "<BR><BR>Cordialement<BR><BR>GUI"

 'Liste des destinataires
 With NewMail
 .Recipients.Add (Sheets("FICHIERS TXT").Range("Y1").Value)

 'Sujet :
 .Subject = "Detail - " & " " & Sheets("FICHIERS TXT ").Range("X1").Value
 'Corps du mail :
 .HTMLBody = Debut & RangetoHTML(rng) & Fin
 
 .Attachments.Add ActiveWorkbook.FullName
 .Display
 'Envoi :
 .Send
 End With
 End Sub

A te relire

Martial
 

Discussions similaires

Réponses
6
Affichages
305

Statistiques des forums

Discussions
312 215
Messages
2 086 330
Membres
103 188
dernier inscrit
evebar