Delete doc when macro finishes

守給你的承諾、 提交于 2019-12-02 01:07:39
The Benetrator

Late reply, I know.

Another way to achieve this for people not familiar with VBScripts might be to put something like this at the end of the macro:

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

EDIT: An even lighter way (Tested on 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!