J'ai écrit le code VBA pour Microsoft Word pour envoyer un courriel quand un bouton est cliqué. Lorsque le bouton est cliqué, et la macro est terminée, je voudrais que le fichier soit supprimé du bureau.Fonction VBA Kill ne pas supprimer le fichier
Lorsque j'exécute maintenant cette macro, Microsoft Word se ferme, mais le fichier n'est pas supprimé de mon bureau.
Voici mon code:
Private Sub CommandButton1_Click()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Dim FileName As String
Dim FilePath As String
Dim DeletePath As String
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
myFileName = "Form"
FilePath = "C:\Users\" & Environ("Username") & "\desktop\"
Doc.SaveAs2 FileName:=FilePath & myFileName & ".docx", Fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False
With EmailItem
.Subject = "Bid Award Form"
.Body = "Please Review the attached Bid Award form"
.To = "[email protected]"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.FullName
.Send
End With
'display a message using named arguments
MsgBox _
prompt:="Your email has been sent. Please check your Outlook sent mail for confirmation", _
Buttons:=vbOKOnly, _
Title:="Email Confirmation"
'Close the File
Doc.Close
Kill "C:\Users\" & Environ("Username") & "\desktop\form.docx"
'Close the Application of the document we are going to delete
Application.Quit
Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub
Toute aide que vous pouvez fournir sera très utile!
vous ne pouvez pas supprimer un fichier qui est ouvert dans une application – jsotola