VBA - Envoyer un mail

Résolu/Fermé
ChrisKad Messages postés 7 Date d'inscription lundi 13 avril 2015 Statut Membre Dernière intervention 17 avril 2015 - Modifié par crapoulou le 17/04/2015 à 16:52
f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 - 21 avril 2015 à 09:29
Bonjour,

J'essaie d'envoyer par mail une partie du tcd via vba mais tout ce que ça fait c'est que ça me crée un fichier excel avec la partie que j'ai sélectionné, mais ça n'envoie pas au destinataire.

Voici le tcd:





J'ai utilisé ce code mais ça n'envoie toujours pas. Ça me crée juste un autre fichier excel avec la colonne "B3:B8"':

Sub Mail_Range()

    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object

    Set Source = Nothing
    On Error Resume Next
    Set Source = Range("B3:B8").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please   correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Tableau stat"

    If Val(Application.Version) < 12 Then
    
       FileExtStr = ".xlsx": FileFormatNum = 51
    End If

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "x"
            .CC = "x+1"
            .BCC = "x+2"
            .Subject = "Table statistique"
            .Body = "This is a test"
            .Attachments.Add Dest.FullName
            
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub


Merci.

Chris
A voir également:

1 réponse

f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701
Modifié par crapoulou le 17/04/2015 à 16:51
Bonjour,
remplacez:
.Attachments.Add Dest.FullName

par
.Attachments.Add TempFilePath & TempFileName & FileExtStr
0
Bonjour,

Je l'ai remplacé et j'ai mit les adresses mails correctes mais ça ne marche toujours pas
0
f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701 > Chriskad
21 avril 2015 à 09:29
Bonjour,

mais ça ne marche toujours pas Qu'est-ce qui ne marche pas ?????
0