宏完成时删除文档 [英] Delete doc when macro finishes

查看:24
本文介绍了宏完成时删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Word 2007 docm file that I send to users as an email attachment to let them update files. The macros download files from a server and install them correctly - as opposed to sending the files as email attachments and trusting users to get it right. I'd use a VBScript file instead, but I'm not allowed to send that in an email.

我想在宏完成后从用户的计算机中删除此文档.但是当它点击任何 Kill 或 FSO.FileDelete 命令时,文档仍然打开并且宏仍在运行.

I would like to delete this document from the user's computer when the macro finishes. But the doc is still open and the macro still running when it hits any Kill or FSO.FileDelete command.

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

edit: found a way to have the Word macro create and launch a VBScript, which fires after the doc closes and deletes the doc and the script.

推荐答案

我知道回复晚了.

对于不熟悉 VBScripts 的人来说,实现此目的的另一种方法可能是在宏的末尾放置类似的内容:

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

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

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

这篇关于宏完成时删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆