Associer mail à un fichier excel

Barethis Messages postés 5 Statut Membre -  
 Jeremie -
Bonjour,

Dans le cadre de mon travail, je suis amené à remplir plusieurs fois par jour un tableau excel et l'envoyer à des collaborateurs en pièce jointe via outlook.

Afin d'éviter de retaper le mail à chaque fois, j'aurais aimé savoir si il était possible de le prédéfinir histoire que le mail apparaisse automatiquement en passant par envoi en tant que pièce jointe via excel.

En espérant avoir été le plus clair possible.
A voir également:

1 réponse

Jeremie
 
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
0