envoi mail selon condition

ngexcel

XLDnaute Occasionnel
Bonsoir

je cherche depuis quelques jours une solution pour envoyer un mail selon la couleur de la cellule
j ai un message quand la macro démarre sur send si quelqu un peut me débloquer je joint le fichier
Encore merci


Option Explicit
Sub Conditions()
Dim Message As String
With Sheets("Feuil1").Range("J9:J97").Activate
If ActiveCell.Interior.ColorIndex = 1 Then EnvoiMail Message
End With
End Sub

Sub EnvoiMail(Message As String)
Dim OutApp As Object, OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Sheets("Feuil1").Range("K3").Value
.Send '<<<<<<<<<<<< Pour envoyer directement
End With
Set OutApp = Nothing: Set OutMail = Nothing
On Error GoTo 0: Err.Clear
Exit Sub
Set OutApp = Nothing: Set OutMail = Nothing
On Error GoTo 0: Err.Clear
Exit Sub
End Sub
 
Dernière édition:
C

Compte Supprimé 979

Guest
Re : envoi mail selon condition

Bonjour ngexcel

Essaye ce code
Code:
Sub EnvoiMailSelonCondition()
  Dim Cel As Range, OutApp As Object, OutMail As Object
  Dim FlgEnvoi As Boolean, Message As String
  ' Initialiser le FLAG
  FlgEnvoi = False
  ' Avec la feuille
  With Sheets("Feuil1")
    For Each Cel In .Range("J9:J97")
      If Cel.Interior.ColorIndex <> 1 Then
        FlgEnvoi = True: Exit For
      End If
    Next Cel
    ' Tester le FLAG
    If FlgEnvoi = True Then
      ' Créer uns instance d'Outlook
      Set OutApp = CreateObject("Outlook.Application")
      ' Créer une instance mail
      Set OutMail = OutApp.CreateItem(0)
      OutMail.To = .Range("K3").Value
      OutMail.Display
      Set OutApp = Nothing: Set OutMail = Nothing
    End If
  End With
End Sub

A+
 

ngexcel

XLDnaute Occasionnel
Re : envoi mail selon condition

bonjour Martial

je dois oublier quelque chose car je une erreur sur l'envoi du mail
merci de votre aide

sub EnvoiMailSelonCondition()
Dim Cel As Range, OutApp As Object, OutMail As Object
Dim FlgEnvoi As Boolean, Message As String
' Initialiser le FLAG
FlgEnvoi = False
' Avec la feuille
With Sheets("Feuil1")
For Each Cel In .Range("J9:J97")
If Cel.Interior.ColorIndex <> 1 Then
FlgEnvoi = True: Exit For
End If
Next Cel
' Tester le FLAG
If FlgEnvoi = True Then
' Créer uns instance d'Outlook
Set OutApp = CreateObject("Outlook.Application")
' Créer une instance mail
Set OutMail = OutApp.CreateItem(0)
OutMail.To = .Range("K3").Value
l.Subject = "Ton Objet"
.Body = "Ton texte, ta date etc..."
OutMail.Send
Set OutApp = Nothing: Set OutMail = Nothing
End If
End With
End Sub
 

Yaloo

XLDnaute Barbatruc
Re : envoi mail selon condition

Re,

Désolé, je n'avais pas vraiment regardé ta macro :
Il faut mettre
OutMail.Subject = "Ton Objet"
OutMail.Body = "Ton texte, ta date etc..."

ou
VB:
sub EnvoiMailSelonCondition()
Dim Cel As Range, OutApp As Object, OutMail As Object
Dim FlgEnvoi As Boolean, Message As String
' Initialiser le FLAG
FlgEnvoi = False
' Avec la feuille
With Sheets("Feuil1")
For Each Cel In .Range("J9:J97")
If Cel.Interior.ColorIndex <> 1 Then
FlgEnvoi = True: Exit For
End If
Next Cel
' Tester le FLAG
If FlgEnvoi = True Then
' Créer uns instance d'Outlook
Set OutApp = CreateObject("Outlook.Application")
' Créer une instance mail
Set OutMail = OutApp.CreateItem(0)
With OutMail
  .To = .Range("K3").Value
  .Subject = "Ton Objet"
  .Body = "Ton texte, ta date etc..."
  .Send
End With
Set OutApp = Nothing: Set OutMail = Nothing
End If
End With
End Sub

A+
 

ngexcel

XLDnaute Occasionnel
Re : envoi mail selon condition

Bonjour et merci de prendre du temps pour moi

j ai un message d 'erreur sur cette ligne

.To = .Range("K3").Value

Errreur d'execution (438)
Propriété ou méthode non gérée par cet objet

une solution ?

merci bonne journée
 
C

Compte Supprimé 979

Guest
Re : envoi mail selon condition

Bonjour ngexcel

lorsque tu codes, le point devant une instruction veut dire que tu travailles avec l'objet définit par "With"

Dans le
Code:
With OutMail
tu travailles avec l'objet mail
donc le ".Range("K3").Value" ne peut pas fonctionner il faut ajouter l'objet feuille devant
Code:
.To = Sheets("Feuil1").Range("K3").Value

A+
 
C

Compte Supprimé 979

Guest
Re : envoi mail selon condition

Re,

Chez moi ce code fonctionne
Code:
Sub EnvoiMailSelonCondition()  Dim Cel As Range, OutApp As Object, OutMail As Object
  Dim FlgEnvoi As Boolean, Message As String
  ' Initialiser le FLAG
  FlgEnvoi = False
  ' Avec la feuille
  With Sheets("Feuil1")
    For Each Cel In .Range("J9:J97")
      If Cel.Interior.ColorIndex <> 1 Then
        FlgEnvoi = True: Exit For
      End If
    Next Cel
    ' Tester le FLAG
    If FlgEnvoi = True Then
      ' Reset du FLAG
      FlgEnvoi = False
      ' Créer uns instance d'Outlook
      Set OutApp = CreateObject("Outlook.Application")
      ' Créer une instance mail
      Set OutMail = OutApp.CreateItem(0)
      OutMail.To = .Range("K3").Value
      OutMail.Subject = "Ton Objet"
      OutMail.Body = "Ton texte, ta date etc..."
      OutMail.Send
      Set OutApp = Nothing: Set OutMail = Nothing
    End If
  End With
End Sub

A+
 

Pièces jointes

  • ngexcel_SécuDef.xlsm
    18.8 KB · Affichages: 18

Statistiques des forums

Discussions
312 756
Messages
2 091 772
Membres
105 068
dernier inscrit
celome