Re, bonjour toutes et tous
un petit Error on Resume Next à mettre avec prudence pour le code Mailing
et CreateObjet au lieu de GetObject
une apostrophe ' ' devant le Set
'Set ObjOutlook = GetObject(, "Outlook.Application") 'vérification si Outlook est ouvert
Je n'ai plus ces 2 erreurs citées ci-dessus
Question : Est ce que c'est ce que vous souhaitez : si l’application Outlook est ouverte alors,.Outlook s'ouvre avec le(s) destinataire(s), le corps du message etc .etc. ?
@ tester
Option Base 1
Public CheminRECAP$
Public CheminJL$
Sub ArreteStockJournalier()
Sheets("ETAT").Activate
Sheets("ETAT").Calculate
Call EditionJL
Call EditionRECAP
Call Reinitialisation
If MsgBox("L'édition du dernier stock connu a été réalisé avec Succès !" & vbCrLf & _
"Le fichier a bien été réinitialisé !" & vbCrLf & vbCrLf & "Voulez-vous envoyer les informations par mail ?", vbYesNo) = vbYes Then
Call EnvoiMail
Else: MsgBox "Procédure achevée sans envoi de mail"
End If
End Sub
Sub EditionJL()
Dim Dossier$, Horodatage$, NomFichier$, Extension$
Dossier = ThisWorkbook.Path 'r_pertoire courant o sera cr__ le fichier PDF
Horodatage = WorksheetFunction.Text(Now, "YYYYMMDD-HHMM") 'avec les heures et minutes
NomFichier = "Journal des stocks " & Horodatage
Extension = ".pdf"
CheminJL = Dossier & "\" & NomFichier & Extension
Sheets("JOURNAL").ExportAsFixedFormat Type:=xlTypePDF, Filename:=CheminJL, IgnorePrintAreas:=False
End Sub
Sub EditionRECAP()
Dim Dossier$, Horodatage$, NomFichier$, Extension$
Dossier = ThisWorkbook.Path 'r_pertoire courant o sera cr__ le fichier PDF
Horodatage = WorksheetFunction.Text(Now, "YYYYMMDD-HHMM") 'avec les heures et minutes
NomFichier = "Recap stock " & Horodatage
Extension = ".pdf"
CheminRECAP = Dossier & "\" & NomFichier & Extension
Sheets("ETAT").ExportAsFixedFormat Type:=xlTypePDF, Filename:=CheminRECAP, IgnorePrintAreas:=False
End Sub
Sub Reinitialisation()
Dim Tampon()
Dim NBL%
NBL = Range("RECAP").Rows.Count
ReDim Tampon(NBL)
Call Unprotection(Sheets("ETAT"))
Call Unprotection(Sheets("JOURNAL"))
Tampon = Range("RECAP[STOCK FINAL]")
'Sheets("ETAT").Activate
Range("RECAP[STOCK CAVE]").Value = Tampon
'Sheets("JOURNAL").Activate
Range("Journal[[DATE MOUVEMENT]:[SORTIES]]").ClearContents
Call Protection(Sheets("ETAT"))
Call Protection(Sheets("JOURNAL"))
End Sub
Sub EnvoiMail()
Dim ObjOutlook As Object 'variable Outlook
Dim ObjMail As Object 'variable mail d'outlook
Dim PJ_RECAP$, PJ_JOURNAL$ 'variables : chemins des PJ ˆ ajouter
Dim LigneD$, Ligne1$, Ligne2$, Ligne3$, LigneF$, contenu$ 'variables pour contenu du mail
Dim AlertePJ$
'---------------------------------------------------------
'-----RECUPERATION DES CHEMINS COMPLETS DES PJ A IMPRIMER (EDITEES A L'INSTANT, AVANT LA REINITIALISATION DU FICHIER)
If MsgBox("Voulez-vous joindre automatiquement le dernier état édité ?" & vbCrLf & vbCrLf & _
"Sinon, pour sélectionner vous-même un fichier, cliquez sur NON.", vbYesNo) = vbYes Then
PJ_RECAP = CheminRECAP
'PJ_JOURNAL = CheminJL
If PJ_RECAP = "" Then AlertePJ = "Attention, détection de PJ manquante(s) !" 'Or PJ_JOURNAL = "" Then GoTo SiErreur
'-----Alternative pour envoyer les PJ d'aprs une sŽlection manuelle
Else:
PJ_RECAP = Application.GetOpenFilename("Fichier pdf (*.pdf), *.pdf") 'pour les pdf
'PJ_JOURNAL = Application.GetOpenFilename("Fichier pdf (*.pdf), *.pdf") 'pour les pdf
If PJ_RECAP = "Faux" Then AlertePJ = "Attention, détection de PJ manquante(s) !" 'Or PJ_JOURNAL = "Faux" Then GoTo SiErreur
End If
'On Error GoTo SiErreur
'---------------------------------------------------------
'-----INITIALISATION D'OUTLOOK - INSTANCIATION
'Set ObjOutlook = GetObject(, "Outlook.Application") 'vérification si Outlook est ouvert
If ObjOutlook Is Nothing Then 'si Outlook n'est pas ouvert, une instance est ouverte
Shell "Outlook.exe", vbNormalFocus 'vbHide
Set ObjOutlook = CreateObject("Outlook.Application")
End If
Set ObjMail = ObjOutlook.CreateItem(0)
'---------------------------------------------------------
'-----CONTENU DU MAIL
LigneD = Range("LigneDebut").Value & Chr(10) & Chr(10) 'les caracteres Chr indique un saut de ligne'
Ligne1 = Range("Ligne1").Value
Ligne2 = Range("Ligne2").Value
Ligne3 = Range("Ligne3").Value
LigneF = Chr(10) & Chr(10) & Range("LigneFin").Value & Chr(10) & Chr(10)
contenu = LigneD & Ligne1 & Ligne2 & Ligne3 & LigneF
'---------------------------------------------------------
'-----CARACTERISTIQUES DU MAIL
With ObjMail
On Error Resume Next
.To = Range("Destinataire").Value 'Destinataire
If Range("DestinataireCC").Value <> "" Then .Cc = Range("DestinataireCC").Value 'Destinataire copie
If Range("DestinataireCCI").Value <> "" Then .Cci = Range("DestinataireCCI").Value 'Destinataire de copie invisible pour confirmation d'envoi (= Email de l'expŽditeur)
.Subject = Range("ObjetMail").Value 'Objet
.Body = contenu 'Corps
If PJ_RECAP <> "" Then .Attachments.Add PJ_RECAP 'Ajout de la piece jointe (dernier recap des stock)
'If PJ_JOURNAL <> "" Then.Attachments.Add PJ_JOURNAL 'Ajout PJ du dernier journal
.Display 'Permet d'effectuer une vŽrification (cette ligne peut tre laissŽe en commentaire)
.Send 'Envoi
End With
'---------------------------------------------------------
'-----FERMETURE OUTLOOK ET LIBERATION DES VARIABLES OBJET - FIN DE PROCEDURE
ObjOutlook.Quit
If Not ObjMail Is Nothing Then Set ObjMail = Nothing
If Not ObjOutlook Is Nothing Then Set ObjOutlook = Nothing
MsgBox "Votre mail a bien été envoyé" & vcbrlf & vcbrlf & AlertePJ
Exit Sub
SiErreur: 'Fin alternative si une erreur s'est produite durant la procŽdure
ObjOutlook.Quit
If Not ObjMail Is Nothing Then Set ObjMail = Nothing
If Not ObjOutlook Is Nothing Then Set ObjOutlook = Nothing
MsgBox "Échec de l'envoi du mail"
End Sub