Delete doc when macro finishes

后端 未结 1 714
太阳男子
太阳男子 2021-01-21 10:55

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 oppose

相关标签:
1条回答
  • 2021-01-21 11:17

    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
    
    0 讨论(0)
提交回复
热议问题