Re : VBA envoi automatique email selon table de correspondance
Bonjour,
J'ai un message d'erreur de Type mismatch avec le nom "Aysel Ayub" voir la ligne ci-dessous et aussi un autre problème avec la référence MISSING: Microsoft Outlook 15.0 Object Library mais je l'ai désactivée et ça a l'air de fonctionné.
P = Application.Match("*" & Nom & "*", Rg, 0) =======> j'ai un message d'erreur de Type mismatch [/B]
Voici le letail des fichiers pour le test que j'ai déposé dans mons C:\data :
1. Project Cost Accounting (Aysel Ayubi) 2014.oct.xls : ( Le nom de "Aysel Ayubi" ne se trouve pas dans ma "table email" il devrait m'être renvoyé en retour)
2. Project Cost Accounting (Mon nom et prénom) 2014.oct.xls ( Ce fichier par contre devrais m'être envoyer dans ma boîte de réception car il se trouve dans ma "table email")
Le détail de ma "table email" pour le test :
Mon nom et prénom = Mon émail
Angelie = autre émail
J'ai repris votre code en modifiant les paramètres que je connais :
==============================================================
Sub test()
Dim Répertoire As String, Fichier As String, Nom As String
Dim Rg As Range, FileToSend As String, P As Long
Dim FeuilInfo As Worksheet, AdresseCourriel As String
'************* Variables à définir ***********
'Nom de la feuille à adapter si nécessaire
Set FeuilInfo = Worksheets("Formulaire")
'********************************* ***********
Répertoire = "C:\Data" ' FeuilInfo.Range("A11")
If Right(Répertoire, 1) <> "\" Then
Répertoire = Répertoire & "\"
End If
Fichier = Dir(Répertoire)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With Worksheets("Table Email")
Set Rg = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With
Fichier = Dir(Répertoire & "*.xl*")
Do While Fichier <> ""
Nom = Nom_Usager(Fichier)
If Nom <> "" Then
P = Application.Match("*" & Nom & "*", Rg, 0) =======> j'ai un message d'erreur de Type mismatch
If IsNumeric(P) Then
AdresseCourriel = Rg(P).Offset(, 1)
FileToSend = Répertoire & Fichier
Else
Err.Clear
AdresseCourriel = FeuilInfo.Range("A14")
FileToSend = Répertoire & Fichier
End If
Else
AdresseCourriel = FeuilInfo.Range("A14")
FileToSend = Répertoire & Fichier
End If
EnvoiCourriel FeuilInfo, AdresseCourriel, FileToSend
Fichier = Dir()
Loop
Set Rg = Nothing: Set FeuilInfo = Nothing
Set objOutlook = Nothing: Set objMail = Nothing
End Sub
Sub EnvoiCourriel(FeuilInfo As Worksheet, AdresseCourriel As String, FileToSend As String)
With objMail
.to = AdresseCourriel
.Subject = CStr(FeuilInfo.Range("A2"))
.Body = CStr(FeuilInfo.Range("A5"))
If FileToSend <> "" Then
.Attachments.Add FileToSend
End If
.Send
'.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
End Sub
Function Nom_Usager(NomFichier As String) As String
Dim Y As Long, Z As Long, GestionErreur As String
On Error GoTo GestionErreur
With Application
Y = .Find("(", NomFichier)
Z = .Find(")", NomFichier) - 1
End With
Nom_Usager = Mid(NomFichier, Y + 1, Z - Y)
Exit Function
GestionErreur:
Err.Clear
Nom_Usager = ""
End Function
=========================================================