Associer mail à un fichier excel
Fermé
Barethis
Messages postés
2
Date d'inscription
mercredi 5 juin 2013
Statut
Membre
Dernière intervention
17 février 2018
-
5 juin 2013 à 11:16
Jeremie - 10 juin 2013 à 15:40
Jeremie - 10 juin 2013 à 15:40
A voir également:
- Associer mail à un fichier excel
- Liste déroulante excel - Guide
- Fichier rar - Guide
- Formule excel - Guide
- Fichier host - Guide
- Creer adresse mail - Guide
1 réponse
Bonjour ! Je te conseille de créer un tableau sur Excel dont tu vas ensuite te servir comme contenu dans Outlook. Supposons que tu l'as créé dans l'onglet « Feuille1 » utilisant comme rangées « A1 :L30 » et les cases « M1 » et « M2 » contiennent les destinataires (TO et CC) tandis que la case « M3 » contient l'intitulé du mail. Tu verras que la macro te donne également l'option d'envoyer ton fichier Excel en pièce jointe.
Puis copie-colle ci-dessous dans l'éditeur VB d'Excel. N'oublie pas de référencer la version Outlook que tu utilises (ALT+F11 -> References -> Coche « Microsoft Outlook XX Object Library »)
Sub Mail()
Application.DisplayAlerts = False
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim Liste_de_diffusion As String
Dim Liste_de_copy As String
Dim Subject As String
Dim Myname
Const olFormatHTML = 2
'Liste_de_diffusion = ActiveSheet.Range("M1").Value
'Liste_de_copy = ActiveSheet.Range("M2").Value
Subject = ActiveSheet.Range("M3").Value
Myname = Format(Now(), "dd/mm/yy")
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
`Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a range if you want
Set rng = Sheets("YourSheet").Range("A1:L30").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", 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
'.From = ""
'.To = Liste_de_diffusion
'.CC = Liste_de_copy
'.BCC = ""
.Subject = Subject & " " & Myname
.BodyFormat = olFormatHTML
.HTMLBody = RangetoHTML(rng)
'.Attachments.Add ("C:\Ticket Backlog Status & Log.rar")
'.Send 'or use
.Display
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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
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
Puis copie-colle ci-dessous dans l'éditeur VB d'Excel. N'oublie pas de référencer la version Outlook que tu utilises (ALT+F11 -> References -> Coche « Microsoft Outlook XX Object Library »)
Sub Mail()
Application.DisplayAlerts = False
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim Liste_de_diffusion As String
Dim Liste_de_copy As String
Dim Subject As String
Dim Myname
Const olFormatHTML = 2
'Liste_de_diffusion = ActiveSheet.Range("M1").Value
'Liste_de_copy = ActiveSheet.Range("M2").Value
Subject = ActiveSheet.Range("M3").Value
Myname = Format(Now(), "dd/mm/yy")
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
`Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a range if you want
Set rng = Sheets("YourSheet").Range("A1:L30").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", 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
'.From = ""
'.To = Liste_de_diffusion
'.CC = Liste_de_copy
'.BCC = ""
.Subject = Subject & " " & Myname
.BodyFormat = olFormatHTML
.HTMLBody = RangetoHTML(rng)
'.Attachments.Add ("C:\Ticket Backlog Status & Log.rar")
'.Send 'or use
.Display
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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
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