OVH Cloud OVH Cloud

Email avec Outlook + piece jointe

1 réponse
Avatar
Raphael
Bonjour,

J'explique en quelques mots ma situation :
J'ai un fichier excel 150 contacts (lignes)
1 ligne est composé de :
Nom du contact, Adresse email du contact, Adresse Email de son responsable,
Ordre d'envoie d'email (0/1), fichier PJ 1, fichier PJ2, Fichier PJ3...

Actuellement, j'ai une macro qui permet d'envoyer des emails à la chaine
mais toute intervention sur le pc pendant que ma macro boucle fait 'planté'
l'envoie d'email.

je souhaiterai que l'ensemble du programme se deroule sans 'sendkey' ni
temporisation.

Exist il un moyen pour passer directement d'excel à l'email sans passé par
Outlook?

Mon code actuellement (il n'y a pas encore de verification dedans) :

Sub sendmail()

Dim Email As String, Subj As String
Dim Msg As String, URL As String
Dim r As Integer, x As Double
Dim PJ(3) As String


For r = 11 To 160
Email = Cells(r, 5) 'Destinataire
CP_Email = Cells(r, 6) 'Destinataire Copie

' Message subject
Subj = "Activité commercial " & Cells(r, 1)

' Compose the message
Msg = ""
Msg = Msg & "Bonjour " & Cells(r, 1) & "," & vbCrLf & vbCrLf
Msg = Msg & "Merci de trouvez ci-joint les fichiers de synthese sur
l'activité commercial " & vbCrLf & vbCrLf
Msg = Msg & "Parametres :" & vbCrLf
Msg = Msg & "- Courtier : " & Cells(r, 4) & vbCrLf
Msg = Msg & "- Secteur : " & Cells(r, 3) & vbCrLf & vbCrLf

Msg = Msg & "Cordialement," & vbCrLf & vbCrLf

' Replace spaces with %20 (hex)
Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")

' Replace carriage returns with %0D%0A (hex)
Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")

' Create the URL
URL = "mailto:" & Email & "?subject=" & Subj & "&cc=" & CP_Email &
"&body=" & Msg

' recuperation des chemins des PJ
j = 0
For i = 1 To 3
If Cells(r, i + 7).Value = 1 Then
PJ(j) = Cells(r, i + 10).Value
j = j + 1
End If
Next i

' Execute the URL (start the email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString,
vbNormalFocus

' Ajout de PJ
For i = 0 To j - 1
PJ_tot = PJ_tot & ";" & PJ(i)


Next i
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "%i"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "f"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys PJ_tot, True 'A ce stade le programme attend un nom de
fichier
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "{ENTER}", True ' et on valide ce nom de fichier
Application.Wait (Now + TimeValue("0:00:02"))


' Wait two seconds before sending keystrokes
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "^~"
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "{ESC}"
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "~"
' Next r
End Sub

****************

1 réponse

Avatar
Raphael
Re-Bonjour,


Voila un fonction que j'ai trouvé dans une FAQ et que j'ai remis à ma
sauce :

Sub envoimail()

Dim Ol As Object
Dim Email As Object
Dim wd As Object

For r = 11 To 160
' Get the email address
DE_Email = Cells(r, 5) 'Destinataire
CP_Email = Cells(r, 6) 'Destinataire Copie

' Création objet Application Outlook
Set Ol = CreateObject("outlook.application")

' Création objet Nouveau message
Set Email = Ol.createItem(olMailItem)

' Liste de diffusion : A
Set Recipient = Email.Recipients
Email.Recipients.Add DE_Email
Email.Recipients.Add CP_Email

' Objet
Email.Subject = "Activité commercial de : " & Cells(r, 1) & "
- " & Cells(r, 2)

' Attachement de la pièce jointe

Set Attachment = Email.Attachments
For i = 1 To 3
If Cells(r, i + 8).Value = 1 Then
Attachment.Add Cells(r, i + 11).Value
End If
Next i

' Corps du message
Msg = ""
Msg = Msg & "Bonjour " & Cells(r, 1) & "," & vbCrLf & vbCrLf
Msg = Msg & "Merci de trouvez ci-joint les fichiers de synthese
sur l'activité commercial " & vbCrLf & vbCrLf
Msg = Msg & "Parametres :" & vbCrLf
Msg = Msg & "- Courtier : " & Cells(r, 4) & vbCrLf
Msg = Msg & "- Secteur : " & Cells(r, 3) & vbCrLf & vbCrLf
Msg = Msg & "Cordialement," & vbCrLf & vbCrLf
Msg = Msg & "Raphaël XXXXXXX" & vbCrLf
Email.body = Msg

' Affichage mail
Email.Send

Next

End Sub

cette fonction me convient beaucoup plus car il n'y a plus de Sendkey
mais il y a toujours une action à faire manuellement:
MS Outlook ouvre une boite de dialogue disant en gros qu'un programme
essaie d'utiliser les adresses de messagerie que j'ai en memoire. il
veut confirmation que cette action est bien autorisé.

Avez vous un moyen d'avoir un shunt de cette protection uniquement
pendant le temps ou je fais tournée ma macro?

Merci.

Raphael.