Sub Email_Range(Destinataire As String, Sujet As String, Message As String, Plage As String, Optional DestinataireCopie As String, Optional DestinataireCopieCache As String, Optional ImportanceMessage As Integer, Optional lien_piece_jointe As String)
Dim rng As Range, Rng1 As Range, Rng2 As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'range à envoyer
If ThisWorkbook.Sheets("Formulaire").Range("AU32").Value = "" Or InStr(Plage, "AX") <> 0 Then
Set rng = ThisWorkbook.Sheets("Formulaire").Range(Plage).SpecialCells(xlCellTypeVisible)
Else
' On traite la première partie du Range
Set Rng1 = ThisWorkbook.Sheets("Formulaire").Range(Left(Plage, InStr(Plage, ",AU") - 1)).SpecialCells(xlCellTypeVisible)
' On traite la première partie du Range
Set Rng2 = ThisWorkbook.Sheets("Formulaire").Range(Right(Plage, Len(Plage) - InStr(Plage, ",AU"))).SpecialCells(xlCellTypeVisible)
Set rng = Application.Union(Rng1, Rng2)
End If
On Error GoTo 0
If rng Is Nothing Then
MsgBox "L'objet que vous voulez envoyé n'est pas du type range ou alors la feuille excel est protégée" & _
vbNewLine & "Message non envoyé. Ré-essayer svp.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Destinataire
.CC = DestinataireCopie
.BCC = DestinataireCopieCache
.Subject = Sujet
.Importance = ImportanceMessage '1:Priorité normale(valeur par défaut)/ 2: priorite haute/ 0 et autres valeurs: faible/
.HTMLBody = Message & RangetoHTML(rng)
.Attachments.Add lien_piece_jointe
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
'------------------------------------------------------------------------------------------------------------------------------------
End Sub
Function RangetoHTML(rng As Range)
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"
' Copie des cellules et création d'un nouveau Workbook pour coller les données
rng.copy
' Sortie de la Function sans autre préavis
' Sortie de la Function sans autre préavis
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
'Publie la feuille au fichier htm
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
'Lecture de toutes les données de htm dans RangetoHIML
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=")
'fermeture de TempWB
TempWB.Close Savechanges:=False
'supprime le fichier htm crée
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function