Copier cellule excel dans mail Outlook

thomasdu40

XLDnaute Occasionnel
Bonjour,

J'ai trouvé et modifié ce code afin de pouvoir, une fois qu'une plage de cellules a été sélectionnée dans un fichier Excel, qu'elle se recopie automatiquement dans un mail Outlook et via un bouton. Ce bouton ouvre un mail Outlook avec certaines champs de complétés comme le destinataire, l'objet.

Le problème c'est que dans ce mail j'aimerai que dans le corps apparaisse automatiquement une phrase du style "en tant que responsable de suivi, certaines de vos actions SMS et de vos anomalies ne sont pas complétées ce jour." J'ai déclaré la variable "Dim StrBody As String" et intégré le code
Code:
.StrBody = "Bonjour," & Chr(13) & "" & Chr(13) & "en tant que responsable de suivi, certains de vos actions SMS et de vos anomalies ne sont pas complétées ce jour."

et ça marche pas. :mad:
Si vous pouvez m'aider. Merci beaucoup.

Code:
Sub Mail_Selection_Range_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim StrBody As String

    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("D4:D12").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
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Alerte : actions SMQ en retard"
        .StrBody = "Bonjour," & Chr(13) & "" & Chr(13) & "en tant que responsable de suivi, certaines de vos actions SMS et de vos anomalies ne sont pas complétées ce jour." 
        .HTMLBody = StrBody & RangetoHTML(rng)
        .Display  '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
 

thomasdu40

XLDnaute Occasionnel
Re : Copier cellule excel dans mail Outlook

Problème résolu.

Après enquête j'ai supprimé les codes suivants
Code:
Dim StrBody As String
,
Code:
.StrBody = "Bonjour," & Chr(13) & "" & Chr(13) & "en tant que responsable de suivi, certaines de vos actions SMS et de vos anomalies ne sont pas complétées ce jour."
et j'ai modifié le code suivant
Code:
.HTMLBody = StrBody & RangetoHTML(rng)
pour le remplacer par
Code:
.HTMLBody = "Bonjour," & "<br>" & "" & "<br>" & "En tant que responsable de suivi, certaines de vos actions SMS ne sont pas complétées ce jour." & "<br>" & "" & "Merci de bien vouloir les compléter ou de relancer les acteurs concernés." & "<br>" & "" & "<br>" & RangetoHTML(rng)

Voci le code final complet :
Code:
Sub Mail_Selection_Range_Outlook_Body()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    

    Set rng = Nothing
    On Error Resume Next
    
    Set rng = Selection.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
        .To = "d.maufrand@delpeyrat.fr"
        .CC = ""
        .BCC = ""
        .Subject = "Alerte : actions SMQ en retard"
        

        .HTMLBody = "Bonjour," & "<br>" & "" & "<br>" & "En tant que responsable de suivi, certaines de vos actions SMS ne sont pas complétées ce jour." & "<br>" & "" & "Merci de bien vouloir les compléter ou de relancer les acteurs concernés." & "<br>" & "" & "<br>" & RangetoHTML(rng)
        .Display  'ou utiliser .Sent
    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"
 
    
    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
 
    
    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
 
    
    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=")
 
    
    TempWB.Close savechanges:=False
 
    
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Discussions similaires

Réponses
2
Affichages
259
Réponses
6
Affichages
308

Statistiques des forums

Discussions
312 298
Messages
2 086 975
Membres
103 416
dernier inscrit
SEB28110