Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Excel > Macros et VBA Excel
Macros et VBA Excel Vos questions relatives aux macros Excel, à l'utilisation de VBA et à l'automatisation de vos classeurs Excel.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 15/11/2011, 09h08   #1
Nouveau Membre du Club
 
Inscription : mars 2007
Messages : 140
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 140
Points : 35
Points : 35
Par défaut Envoi feuilles Excel par mail

Bonjour,

Je cherche à transmettre des feuilles excel par mail, et je ne trouve pas vraiment ce que je veux sur le net. j'ai trouvé de bouts de codes sur ce forum pour lesquels j'ai besoin de les consolider afin d'obtenir le résultat que je souhaites.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Sub envoi_Mail()
 
'il faut obligatoirement ajouter dans les références microsoft outlook
Dim vEmail As Outlook.MailItem
Dim OlApp As Outlook.Application
Dim vFichier As String
Dim wbk As Workbook
 
Set vAppOL = CreateObject("Outlook.application")
Set vEmail = vAppOL.CreateItem(olMailItem)
 
ActiveWorkbook.Sheets(Array("Feuil1", "Feuil2")).Copy
    Set wbk = ActiveWorkbook
 
With vEmail
    .To = "xxx@xx.com"
    .CC
    'Pour joindre un fichier
    .Attachments.Add wbk
    .Subject = "Droit AGIRH pour test"
    .BodyFormat = olFormatHTML
        .HTMLBody = "<HTML><body>Ce <b>mot</b> est en gras.<p>" _
        & "Ce " & "<u>mot</u> est souligné.<p>" & "Ce " _
        & "<FONT COLOR=RED>mot</FONT> est en rouge.<p>" _
        & "Ce " & "<i>mot</i> est en italique.<p>" & _
        "<b><i><u><font color=green>Ce format est vert, gras, italique et souligné !</font></u></i></b></body><HTML>"
        .Display 'Ou .Send
End With
Set vEmail = Nothing
wbk.Close savechanges:=False
 
 
End Sub
ce code bloque sur ".Attachments.Add wbk" ?

par avance merci pour votre aide
meumeu73.1 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 15/11/2011, 14h21   #2
Nouveau Membre du Club
 
Inscription : mars 2007
Messages : 140
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 140
Points : 35
Points : 35
Re,

J'ai trouvé la solution sur le web :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Sub Mail_Sheets_Array()
'Working in 2000-2010
    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 OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim TheActiveWindow As Window
    Dim TempWindow As Window
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
 
    Set Sourcewb = ActiveWorkbook
 
    'Copy the sheets to a new workbook
    'We add a temporary Window to avoid the Copy problem
    'if there is a List or Table in one of the sheets and
    'if the sheets are grouped
    With Sourcewb
        Set TheActiveWindow = ActiveWindow
        Set TempWindow = .NewWindow
        .Sheets(Array("Feuil1", "Feuil2")).Copy
    End With
 
    'Close temporary Window
    TempWindow.Close
 
    Set Destwb = ActiveWorkbook
 
    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010, we exit the sub when your answer is
            'NO in the security dialog that you only see  when you copy
            'an sheet from a xlsm file with macro's disabled.
            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
 
    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " _
                 & Format(Now, "dd-mmm-yy h-mm-ss")
 
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
 
    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = "****@****"
            .CC = ""
            .BCC = ""
            .Subject = "Test"
            .BodyFormat = olFormatHTML
                .HTMLBody = "<HTML><body>Ce <b>mot</b> est en gras.<p>" _
                & "Ce " & "<u>mot</u> est souligné.<p>" & "Ce " _
                & "<FONT COLOR=RED>mot</FONT> est en rouge.<p>" _
                & "Ce " & "<i>mot</i> est en italique.<p>" & _
                "<b><i><u><font color=green>Ce format est vert, gras, italique et souligné !</font></u></i></b></body><HTML>"
            .Display 'création du mail
            '.Send ' envoi du mail
 
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send   'or use .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
 
    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr
 
    Set OutMail = Nothing
    Set OutApp = Nothing
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
Pour plus de précision : http://www.rondebruin.nl/sendmail.htm
meumeu73.1 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 15/11/2011, 19h05   #3
Expert Confirmé
 
Avatar de patricktoulon
 
patrick
Inscription : avril 2009
Messages : 1 829
Détails du profil
Informations personnelles :
Nom : patrick
Âge : 42
Localisation : France, Var (Provence Alpes Côte d'Azur)

Informations professionnelles :
Secteur : Bâtiment

Informations forums :
Inscription : avril 2009
Messages : 1 829
Points : 2 857
Points : 2 857
Envoyer un message via MSN à patricktoulon
Par défaut heu...!!!

bonjour

regarde mon mailer le lien est dans ma signature

j'utilise la méthode "cdo"

et envoie en pièce jointe un classeur pré sauvé au format pdf

tu devrais trouver ton bonheur


au plaisir
__________________
mes fichiers dans les contributions:
mon formulaire mail avec CDO en vba et mon formulaire mail avec CDO en vbs dans un HTA
mon nouveau mouse in out pour les boutons dans un userform
mon addin pour prendre un cliché de selection de cellules

si ton problème est résolu n'oublie pas de pointer :résolu:ça peut servir aux autres
et n'oublie pas de voter
patricktoulon est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/11/2011, 07h33   #4
Nouveau Membre du Club
 
Inscription : mars 2007
Messages : 140
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 140
Points : 35
Points : 35
Merci bien, je regarderai cette solution également, car je ne sais pas encore si le destinataire préfère recevoir un excel ou un pdf.

cdt

meumeu73
meumeu73.1 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/11/2011, 17h40   #5
Expert Confirmé
 
Avatar de patricktoulon
 
patrick
Inscription : avril 2009
Messages : 1 829
Détails du profil
Informations personnelles :
Nom : patrick
Âge : 42
Localisation : France, Var (Provence Alpes Côte d'Azur)

Informations professionnelles :
Secteur : Bâtiment

Informations forums :
Inscription : avril 2009
Messages : 1 829
Points : 2 857
Points : 2 857
Envoyer un message via MSN à patricktoulon
Par défaut re

bonjour meumeu

en fait le format pdf est bien dans le sens ou tu envoie un document et que tu ne veux pas qu'il soit modifié maintenant tu peux toujours envoyer au format xls ou ce que tu veux

au plaisir
__________________
mes fichiers dans les contributions:
mon formulaire mail avec CDO en vba et mon formulaire mail avec CDO en vbs dans un HTA
mon nouveau mouse in out pour les boutons dans un userform
mon addin pour prendre un cliché de selection de cellules

si ton problème est résolu n'oublie pas de pointer :résolu:ça peut servir aux autres
et n'oublie pas de voter
patricktoulon est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 09h51.


 
 
 
 
Partenaires

Hébergement Web