VBA Kill功能不删除文件

时间:2017-08-30 18:19:22

标签: vba ms-word

我已经为Microsoft Word编写了VBA代码,以便在单击按钮时发送电子邮件。单击按钮,宏完成后,我希望从桌面删除该文件。

现在运行此宏时,Microsoft Word会关闭,但该文件不会从我的桌面删除。

这是我的代码:

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 = "EMAILADDRESS@DOMAIN.COM"
    .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

您可以提供的任何帮助都非常有帮助!

2 个答案:

答案 0 :(得分:0)

可能你看错了文件。 Kill是一个功能,不会出错。 只需尝试以下方法:

Sub KillTheFile
    Kill "C:\Users\" & Environ("Username") & "\desktop\form.docx"
End Sub

然后看看它是否有效。可能你有一个文件frm.docx或类似的文件。

答案 1 :(得分:0)

尝试这样做

On Error Goto 0
Dim sDocName as string
sDocName = Doc.FullName
Doc.Close
Kill sDocName