Envoyer un Email avec un fichier joint

BOCARAMEL

XLDnaute Occasionnel
Bonjour a tous :)

Aprés de multiple recherche j'ai réussi a faire un bout de code
création d'un classeur avec une feuille de mon classeur actif

création de l'Email jusque la tout va bien

ça bloque au moment de joindre le fichier dans l'email :confused:

(cela doit fonctionner avec la messagerie par défaut)


merci pour votre aide

bocaramel
 

Pièces jointes

  • classeur_mail.xls
    28 KB · Affichages: 161

pedrag31

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

Bonjour Bocaramel, Bonjour le forum,

Ta macro est incomplete pour pouvoir utiliser le code :
Code:
.Attachments.Add = fich

Ce code s'utilise avec l'application Outlook et il faut avoir au prealable creer un nouvel email...

Code:
Set NouvelEmail = CreateObject("Outlook.Application").CreateItem(0)

With NouvelEmail
.Attachments.Add = fich
End With

Mais une petite recherche sur le forum avec des mots clefs "email avec piece jointe", "email avec attachement", "envoyer mail avec fichier joint", "piece jointe avec oulook 2003", "fichier joint avec outlook express", ou tout betement "outlook", devrait te donner une multitude de pistes...

Ce sujet a ete aborde des dizaines et dizaines de fois, sous toutes ses facettes!;)

Reviens sur le fil si tu n'arrive pas a adapter le fruit de tes recherches...

Bonne journee, :)
 

BOCARAMEL

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

bonjour pedrag31
et merci de ton aide :)

effectivement avec outlook j'ai trouvé plein
d'astuce

moi j'aimerai que ça fonctionne avec n'importe quelle messagerie
installer par defaut sur l'ordi

ton code ne fonctionne pas il me marque
(composant activex ne peut pas créer d'objet)

encore merci
bocaramel :)
 

BOCARAMEL

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

Bonjour PierreJean ;)

je n'envoie pas le fichier actif

je crée un classeur temporaire avec les informations de la feuil2 de mon fichier
actif

et j'aimerai l'envoyer par email

cdlt :)
bocaramel
 

Guiv

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

Re,
Voilà, j'ai retrouvé le fichier exemple avec toutes les possibilités...

Bonne soirée
Guiv

Edit: Oups, c'est un fichier modifié, il faut que je retrouve l'original...
 
Dernière édition:

BOCARAMEL

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

Bonsoir tout le monde
et vraiment merci de votre aide :)

Pierrejean il y a t'il une astuce car chez ça ne marche pas
ça me marque

la méthode 'sendmail de l'objet'_workbook à échoué

je ne comprend pas :confused:

pour les methodes de GuiV et tototiti2008 je creuse pour
trouver la solution

bocaramel
 

Guiv

XLDnaute Occasionnel
Re : Envoyer un Email avec un fichier joint

Bonjour,
Ci-dessous le code pour envoyer une ou plusieurs feuilles en pièce jointe. Il n'y a qu'à changer les parties en rouge.

Testé sur XL2003, mais Ron Debruin affirme sue ça fonctionne sur 2007...

Code:
Option Explicit

[COLOR="SeaGreen"]'This procedure will send the ActiveSheet in a new workbook
'For more sheets use : Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy
[/COLOR]
Sub CDO_Mail_ActiveSheet_Or_Sheets()
[COLOR="SeaGreen"]'Working in 97-2007[/COLOR]
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant

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

    Set Sourcewb = ActiveWorkbook

    [COLOR="SeaGreen"]'Copy the ActiveSheet to a new workbook[/COLOR]
    ActiveSheet.Copy

   [COLOR="SeaGreen"] 'Or if you want to copy more than one sheet use:
    'Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy[/COLOR]

    Set Destwb = ActiveWorkbook

   [COLOR="SeaGreen"] 'Determine the Excel version and file extension/format[/COLOR]
    With Destwb
        If Val(Application.Version) < 12 Then
            [COLOR="SeaGreen"]'You use Excel 97-2003[/COLOR]
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
           [COLOR="SeaGreen"] 'You use Excel 2007
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.[/COLOR]
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    [COLOR="SeaGreen"]'    'Change all cells in Destwb to values if you want
    '    For Each sh In Destwb.Worksheets
    '        sh.Select
    '        With sh.UsedRange
    '            .Cells.Copy
    '            .Cells.PasteSpecial xlPasteValues
    '            .Cells(1).Select
    '        End With
    '        Application.CutCopyMode = False
    '    Next sh
    '    Destwb.Worksheets(1).Select


    'Save the new workbook/Mail it/Delete it[/COLOR]
    TempFilePath = Environ$("temp") & "\"
    TempFileName =[COLOR="Red"] "Extrait de" & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")[/COLOR]

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        .Close savechanges:=False
    End With

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1    [COLOR="SeaGreen"]' CDO Source Defaults[/COLOR]
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = [COLOR="Red"]"smtp.tonserveursmtp.fr"[/COLOR]
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With

    With iMsg
        Set .Configuration = iConf
        .To =[COLOR="Red"]"toto@machin.fr"[/COLOR] [COLOR="SeaGreen"]‘adresse du destinataire[/COLOR]
        .CC = ""
        .BCC = ""
        .From =[COLOR="Red"] """bocaramel""<bocaramel@truc.fr >"[/COLOR] [COLOR="SeaGreen"]‘ton adresse[/COLOR]
        .Subject = [COLOR="Red"]"Essai d’envoi feuille XL"[/COLOR]
        .TextBody = [COLOR="Red"]"Quoi de neuf, docteur?"[/COLOR]
        .AddAttachment TempFilePath & TempFileName & FileExtStr
        .Send
    End With


   [COLOR="SeaGreen"] 'Delete the file you have send[/COLOR]
    Kill TempFilePath & TempFileName & FileExtStr

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


Dis nous si ça fonctionne.

Cordialement,
Guiv
 

Discussions similaires

Statistiques des forums

Discussions
312 493
Messages
2 088 956
Membres
103 990
dernier inscrit
lamiadebz