宏完成时删除doc

时间:2014-07-10 14:23:49

标签: vba word-vba

我有一个Word 2007 docm文件,我将其作为电子邮件附件发送给用户,以便让他们更新文件。宏从服务器下载文件并正确安装 - 而不是将文件作为电子邮件附件发送,并信任用户以使其正确。我改为使用VBScript文件,但我不允许在电子邮件中发送该文件。

我想在宏完成时从用户的计算机中删除此文档。但是当文档命中任何Kill或FSO.FileDelete命令时,文档仍处于打开状态且宏仍在运行。

编辑:找到了一种让Word宏创建并启动VBScript的方法,该文件在doc关闭后触发并删除文档和脚本。

1 个答案:

答案 0 :(得分:2)

迟到的回复,我知道。

为不熟悉VBScripts的人实现此目的的另一种方法可能是在宏的末尾添加类似的内容:

Sub DeleteCurrentDoc()

Dim Doc1 As Document
Dim Doc2 As Document
Dim deletepath As String

'While the document you want to delete is open and is the current 'Active Document'
Set Doc1 = ActiveDocument

'get path of this document to delete it later
deletepath = Doc1.FullName

'Add a new temporary blank document
Set Doc2 = Documents.Add

'Close the document we are going to delete
Doc1.Close False

'Delete it
Kill (deletepath)

'Clear away the temp doc we created
Doc2.Close False

'Tidy up and close Word (Optional Line, delete if necessary)
Application.Quit

End Sub

编辑:更轻松的方式(在Word 2013上测试):

Sub DeleteCurrentDoc()

Dim deletepath As String

'get path of this document to delete it later
deletepath = ActiveDocument.FullName

'Close the document we are going to delete (Word should remain Open
ActiveDocument.Close False

'Delete it
Kill (deletepath)

'Tidy up and close Word (Optional Line, delete if necessary)
Application.Quit

End Sub