【问题标题】:Delete doc when macro finishes宏完成时删除文档
【发布时间】:2020-07-07 04:43:48
【问题描述】:

我有一个 Word 2007 文档文件,我将它作为电子邮件附件发送给用户,让他们更新文件。宏从服务器下载文件并正确安装它们 - 而不是将文件作为电子邮件附件发送并信任用户正确安装。我会改用 VBScript 文件,但我不能通过电子邮件发送它。

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

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    我知道回复晚了。

    对于不熟悉 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 2023-03-24
      • 2019-03-11
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多