Tester si répertoire existe et le créer si non
Hello tlm,
J'ai un bouton dans un formulaire qui à pour fonction:
- Contrôler si le dossier destination existe
- S'il n'existe pas le créer
- faire une copie de mon fichier et le sauver sous un chemin x
- renommer et sauver le fichier ouvert dans le répertoire existant ou créé
- S'il existe déjà, juste faire la copie et la sauvegarde
- Envoi du fichier par mail
Voici mon code:
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
| Sub Button269_Click()
'Check dir and created if necessary, Save & Send the renamed file by email '
If (Dir("C:\Windows\Temp\repertoir_stockage")) <> "repertoir_stockage" Then
MkDir ("C:\Windows\Temp\repertoir_stockage")
ActiveWorkbook.SaveCopyAs filename:="C:\Windows\Temp\" & Range("B7").Value & "_" & Range("I8").Value & "_eShell_order.xls"
ActiveWorkbook.SaveAs filename:="C:\Windows\Temp\Phonak_eShell\" & Range("B7").Value & "_" & Range("I8").Value & "_eShell_order.xls"
Else
End If
Dim ol As Object, myItem As Object
Set ol = CreateObject("outlook.application")
Set myItem = ol.CreateItem(olMailItem)
myItem.to = "moi@mail.com"
myItem.Subject = "German acoustician order form"
myItem.Body = "voici le fichier de stockage"
'fichier en cours d'utilisation envoyé en attaché:
myItem.Attachments.Add ActiveWorkbook.FullName
myItem.Send
Set ol = Nothing
End Sub |
Cela fonctionne très bine à la première execution mais si je l'execute à nouveau ça plante sur: MkDir ("C:\Windows\Temp\repertoir_stockage") car il existe déjà du coup.
Comme lui dire s'il existe déja juste faire:
La copie, renommage et sauvegarde
Code:
1 2
| ActiveWorkbook.SaveCopyAs filename:="C:\Windows\Temp\" & Range("B7").Value & "_" & Range("I8").Value & "_eShell_order.xls"
ActiveWorkbook.SaveAs filename:="C:\Windows\Temp\Phonak_eShell\" & Range("B7").Value & "_" & Range("I8").Value & "_eShell_order.xls" |
Et l'envoi par mail en attachment:
Code:
1 2 3 4 5 6 7 8 9 10
| Dim ol As Object, myItem As Object
Set ol = CreateObject("outlook.application")
Set myItem = ol.CreateItem(olMailItem)
myItem.to = "moi@mail.com"
myItem.Subject = "German acoustician order form"
myItem.Body = "voici le fichier de stockage"
'fichier en cours d'utilisation envoyé en attaché:
myItem.Attachments.Add ActiveWorkbook.FullName
myItem.Send
Set ol = Nothing |
Merci d'avanc epour votre aide!:king: