XL 2019 Envoi TCD par mail

Rabeto

XLDnaute Occasionnel
Bonjour,

J'ai trouvé un code sur le forum qui permet d'envoyer un TCD par mail, (dans module du fichier en PJ)

J'aimerai rajouter quelque spécificité sur le code (Si quelqu'un a une idée :) ) , prendre les valeurs dans les cellules :

Cellule G1 pour les Destinataire
Cellule G2 pour les CC En Copie
Cellule G3 pour le Corp du mail

Merci,
 

Pièces jointes

  • Compter valeur par mois.xlsm
    33.4 KB · Affichages: 8
Solution
Bonjour,
Essaie :
VB:
Sub EnvoyerTCDparMail2()
    Dim TCD As Range, MailApp As Object, Mail As Object, Ws As Worksheet, Destinataire$, Fichier$, CorpsMail$, chemin$
    chemin = ThisWorkbook.Path & "\tcd.png"
    Set Ws = ActiveSheet
    Set TCD = Ws.PivotTables("tcd1").TableRange2
    If TCD Is Nothing Then
        MsgBox "Le tableau croisŽ dynamique 'tableau croisŽ 1' n'existe pas sur la feuille active.", vbExclamation
        Exit Sub
    End If
    Fichier = CopyOBJECTInImagePNG(TCD, chemin, True)    'on copie le tableau en png

    CorpsMail = "<html><body style=""font-family:calibri;font-size:11pt;""><p>" & Range("G3") & "</p><br>Cordialement.<br>"

    ' l'image du tableau
    CorpsMail = CorpsMail & "<img src=""tcd.png""...

danielco

XLDnaute Accro
Bonjour,
Essaie :
VB:
Sub EnvoyerTCDparMail2()
    Dim TCD As Range, MailApp As Object, Mail As Object, Ws As Worksheet, Destinataire$, Fichier$, CorpsMail$, chemin$
    chemin = ThisWorkbook.Path & "\tcd.png"
    Set Ws = ActiveSheet
    Set TCD = Ws.PivotTables("tcd1").TableRange2
    If TCD Is Nothing Then
        MsgBox "Le tableau croisŽ dynamique 'tableau croisŽ 1' n'existe pas sur la feuille active.", vbExclamation
        Exit Sub
    End If
    Fichier = CopyOBJECTInImagePNG(TCD, chemin, True)    'on copie le tableau en png

    CorpsMail = "<html><body style=""font-family:calibri;font-size:11pt;""><p>" & Range("G3") & "</p><br>Cordialement.<br>"

    ' l'image du tableau
    CorpsMail = CorpsMail & "<img src=""tcd.png"" style=""width:" & Round(TCD.Width * 1.15) & "pt;height:" & Round(TCD.Height * 1.15) & "pt;""></img><br><br>"

    'la signature
    CorpsMail = CorpsMail & GetCodeSig("blablabla")    'adapter le nom de la signature

    'fermeture du body
    CorpsMail = CorpsMail & "</body></html>"


    Set MailApp = CreateObject("Outlook.Application")
    Set Mail = MailApp.CreateItem(0)

    Mail.Subject = "Commandes en retard d'expédition"
    Mail.htmlbody = CorpsMail
    Mail.To = Range("G1")
    Mail.Cc = Range("G2")
    Mail.attachments.Add Fichier


    Mail.Display
    'Mail.send
    Set Mail = Nothing
    Set MailApp = Nothing
    Set Ws = Nothing
    Set TCD = Nothing
    Kill Fichier
End Sub

Daniel
 

patricktoulon

XLDnaute Barbatruc
re
bonjour
ha ben elle marche bien ma fonction png même pour les tcds
ça fait plaisir
juste une question
pourquoi "*1.15" pour les dimensions ?
pour le texte G3, il faut faire un replace chr(10) par "<BR>
et le "cordialement" va en dessous le tableau normalement
VB:
Option Explicit
Sub EnvoyerTCDparMail2()
    Dim TCD As Range, MailApp As Object, Mail As Object, Ws As Worksheet, Destinataire$, Fichier$, CorpsMail$, chemin$
    chemin = ThisWorkbook.Path & "\tcd.png"
    Set Ws = ActiveSheet
    Set TCD = Ws.PivotTables("tcd1").TableRange2
    If TCD Is Nothing Then
        MsgBox "Le tableau croisŽ dynamique 'tableau croisŽ 1' n'existe pas sur la feuille active.", vbExclamation
        Exit Sub
    End If
    Fichier = CopyOBJECTInImagePNG(TCD, chemin, True)    'on copie le tableau en png

    CorpsMail = "<html><body style=""font-family:calibri;font-size:11pt;"">" & Replace(Feuil1.[G3].Text, Chr(10), "<br>") & "<br><br>"

    ' l'image du tableau
    CorpsMail = CorpsMail & "<img src=""tcd.png"" style=""width:" & Round(TCD.Width) & "pt;height:" & Round(TCD.Height) & "pt;""></img><br><br>"

    CorpsMail = CorpsMail & "<br>Cordialement.<br>"

    'la signature
    CorpsMail = CorpsMail & GetCodeSig("blablabla")    'adapter le nom de la signature

    'fermeture du body
    CorpsMail = CorpsMail & "</body></html>"


    Set MailApp = CreateObject("Outlook.Application")
    Set Mail = MailApp.CreateItem(0)

    Mail.Subject = "Commandes en retard d'expédition"
    Mail.htmlbody = CorpsMail
    Mail.To = Feuil1.[G1].Text
    Mail.Cc = Feuil1.[G2].Text
    Mail.attachments.Add Fichier


    Mail.Display
    'Mail.send
    Set Mail = Nothing
    Set MailApp = Nothing
    Set Ws = Nothing
    Set TCD = Nothing
    Kill Fichier
End Sub
Function GetCodeSig(ByVal Signature As String) As String
    Dim x%, lines$, i&, Fichier$: x = FreeFile
    Fichier = Environ("appdata") & "\Microsoft\Signatures\" & Signature & ".htm"
    If Dir(Fichier) = "" Then Exit Function
    Open Fichier For Input As #x: lines = Input$(LOF(x), #x): Close #x
    GetCodeSig = lines
End Function

Function CopyOBJECTInImagePNG(ObjecOrRange, _
                              Optional cheminx As String = "", _
                              Optional Notransparency As Boolean = False) As String
    'Autor:patricktoulon
    'https://excel-downloads.com/resources/une-fonction-pour-capturer-un-object-dans-une-feuille-en-png-avec-un-graphique-qui-marche-vraiment.1469/
    Dim Graph As Object, CheminT$
    If cheminx = "" Then cheminx = ThisWorkbook.Path & "\imagetemp.png"
    With CreateObject("htmlfile").parentwindow.clipboardData.clearData("Text"): End With    'on vide le clipboard entre chaque copie pour tester vraiment le available

    ObjecOrRange.CopyPicture Format:=IIf(Notransparency, xlBitmap, xlPicture)
    Set Graph = ObjecOrRange.Parent.ChartObjects.Add(0, 0, 0, 0).Chart
    ActiveSheet.Shapes(Graph.Parent.Name).Line.Visible = msoFalse
    With Graph.Parent
        .Width = ObjecOrRange.Width: .Height = ObjecOrRange.Height: .Left = ObjecOrRange.Width + 20:
        .Select
        Do: DoEvents
            .Chart.Paste
        Loop While .Chart.Pictures.Count = 0

        .Chart.ChartArea.Fill.Visible = msoTrue
        .Chart.ChartArea.Fill.Solid
        .Chart.ChartArea.Format.Fill.Transparency = 1
        .Chart.Export cheminx, "png"
    End With
    Graph.Parent.Delete
    CopyOBJECTInImagePNG = cheminx
End Function
ca donne ça en résultat
1708770418958.png
 

patricktoulon

XLDnaute Barbatruc
ben bien sur
VB:
Option Explicit
Sub EnvoyerTCDparMail2()
    Dim TCD(1 To 3) As Range, MailApp As Object, Mail As Object, Ws As Worksheet, Destinataire$, Fichier$, CorpsMail$, chemin$(1 To 3), i&, texte

    chemin(1) = ThisWorkbook.Path & "\tcd1.png"
    chemin(2) = ThisWorkbook.Path & "\tcd2.png"
    chemin(3) = ThisWorkbook.Path & "\tcd3.png"

    Set Ws = ActiveSheet
    On Error Resume Next
    Set TCD(1) = Ws.PivotTables("tcd1").TableRange2
    Err.Clear
    Set TCD(2) = Ws.PivotTables("tcd2").TableRange2
    Err.Clear
    Set TCD(3) = Ws.PivotTables("tcd3").TableRange2
    On Error GoTo 0
  
    For i = 1 To 3
        If TCD(i) Is Nothing Then
            texte = texte & "Le tableau croisŽ dynamique TCD" & i & " n'existe pas sur la feuille active." & vbCrLf
        End If
    Next
    If texte <> "" Then MsgBox texte, vbExclamation
  
    For i = 1 To 3
        If Not TCD(i) Is Nothing Then Fichier = CopyOBJECTInImagePNG(TCD(i), chemin(i), True)    'on copie le tableau en png
    Next
    CorpsMail = "<html><body style=""font-family:calibri;font-size:11pt;"">" & Replace(Feuil1.[G3].Text, Chr(10), "<br>") & "<br><br>"

    ' l'image du tcd1
    If Not TCD(1) Is Nothing Then CorpsMail = CorpsMail & "<img src=""tcd1.png"" style=""width:" & Round(TCD(1).Width) & "pt;height:" & Round(TCD(1).Height) & "pt;""></img><br><br>"

    ' l'image du tcd2
    If Not TCD(2) Is Nothing Then CorpsMail = CorpsMail & "<img src=""tcd2.png"" style=""width:" & Round(TCD(2).Width) & "pt;height:" & Round(TCD(2).Height) & "pt;""></img><br><br>"

    ' l'image du tcd3
    If Not TCD(3) Is Nothing Then CorpsMail = CorpsMail & "<img src=""tcd3.png"" style=""width:" & Round(TCD(3).Width) & "pt;height:" & Round(TCD(3).Height) & "pt;""></img><br><br>"


    CorpsMail = CorpsMail & "<br>Cordialement.<br>"

    'la signature
    CorpsMail = CorpsMail & GetCodeSig("blablabla")    'adapter le nom de la signature

    'fermeture du body
    CorpsMail = CorpsMail & "</body></html>"


    Set MailApp = CreateObject("Outlook.Application")
    Set Mail = MailApp.CreateItem(0)

    Mail.Subject = "Commandes en retard d'expédition"
    Mail.htmlbody = CorpsMail
    Mail.To = Feuil1.[G1].Text
    Mail.Cc = Feuil1.[G2].Text
    For i = 1 To 3
        If Dir(chemin(i)) <> "" Then Mail.attachments.Add ThisWorkbook.Path & "\tcd" & i & ".png"
    Next

    Mail.Display
    'Mail.send
    Set Mail = Nothing
    Set MailApp = Nothing
    Set Ws = Nothing
    For i = 1 To 3: Set TCD(i) = Nothing: Next
    For i = 1 To 3
    If Dir(chemin(i)) <> "" Then Kill chemin(i)
    Next
End Sub

Function GetCodeSig(ByVal Signature As String) As String
    Dim x%, lines$, i&, Fichier$: x = FreeFile
    Fichier = Environ("appdata") & "\Microsoft\Signatures\" & Signature & ".htm"
    If Dir(Fichier) = "" Then Exit Function
    Open Fichier For Input As #x: lines = Input$(LOF(x), #x): Close #x
    GetCodeSig = lines
End Function

Function CopyOBJECTInImagePNG(ObjecOrRange, _
                              Optional cheminx As String = "", _
                              Optional Notransparency As Boolean = False) As String
'Autor:patricktoulon
'https://excel-downloads.com/resources/une-fonction-pour-capturer-un-object-dans-une-feuille-en-png-avec-un-graphique-qui-marche-vraiment.1469/
    Dim Graph As Object, CheminT$
    If cheminx = "" Then cheminx = ThisWorkbook.Path & "\imagetemp.png"
    With CreateObject("htmlfile").parentwindow.clipboardData.clearData("Text"): End With    'on vide le clipboard entre chaque copie pour tester vraiment le available
    DoEvents
    ObjecOrRange.CopyPicture Format:=IIf(Notransparency, xlBitmap, xlPicture)
    Set Graph = ObjecOrRange.Parent.ChartObjects.Add(0, 0, 0, 0).Chart
    ActiveSheet.Shapes(Graph.Parent.Name).Line.Visible = msoFalse
    With Graph.Parent
        .Width = ObjecOrRange.Width: .Height = ObjecOrRange.Height: .Left = ObjecOrRange.Width + 20:
        .Select
        Do: DoEvents
            .Chart.Paste
        Loop While .Chart.Pictures.Count = 0

        .Chart.ChartArea.Fill.Visible = msoTrue
        .Chart.ChartArea.Fill.Solid
        .Chart.ChartArea.Format.Fill.Transparency = 1
        .Chart.Export cheminx, "png"
    End With
    Graph.Parent.Delete
    CopyOBJECTInImagePNG = cheminx
End Function
 

Discussions similaires

Réponses
2
Affichages
236
Réponses
16
Affichages
517
Réponses
6
Affichages
305

Statistiques des forums

Discussions
312 211
Messages
2 086 296
Membres
103 171
dernier inscrit
clemm