I have a form that is send to users to complete and electronically submit, which really just sends an automated email with the doc as an attachment. In order to attach the doc in the email, it first saves to the users desktop automatically. What I want to do is find a way to delete this document after it's submitted. I found a few examples online but none have worked for me, and frankly I don't see how it would work if you have a line ActiveDocument.Close False followed by the line to delete the file path, the document closes and the macro is no longer running as far as I can imagine.
I found 1 unmarked answer to a question from another user: Delete doc when macro finishes
Sub DeleteCurrentDoc()
Dim Doc1 As Document
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
and another on word.tips.net : https://wordribbon.tips.net/T011642_Deleting_the_Open_Document_File
Sub DeleteThisFile()
Dim MyFile As String
MyFile = ActiveDocument.Path & "\" & ActiveDocument.Name
If MsgBox(MyFile & " will be deleted permanently", _
vbYesNo, "Delete this File?") = vbYes Then
ActiveDocument.Close (wdDoNotSaveChanges)
Kill MyFile
End If
End Sub
I tried to look into a code to open a new document and add a macro to it each time but I feel that is getting into some unwanted territory. I would really like to find a way to delete the file from the user's pc however, as they are to only have access to a single form upon request.
I'm using Word 2013, i'm not sure if perhaps the examples above work in earlier versions and just not mine, Is this possible?
thank you for any help.
UPDATE I'm attempting a workaround currently, where the document will save as a read only file for the user after it sends the attachment, this way they can save for their records I suppose. Though the protection I'm setting in the workbook to make it read only is not applying, instead it is reverting back to the forms only protection of the original file and I cannot find out why.
`ActiveDocument.SaveAs2 FileName:="C:\Users\" & curUser & "\Desktop\My User Request_" & _
Format(Date, "mm.dd.yy") & ".docx", fileformat:=wdFormatDocumentDefault
Selection.HomeKey wdStory
With ActiveDocument
.Protect 3, Password:="password"
.Save
End With`
What I'm doing here is re-saving it with a different file name and as a non macro enabled workbook, I then delete the temporary file I originally saved to their desktop required to send the attachment, leaving them with only a copy of the form they filled out which they can no longer make any changes to.
I've tried a few ways where I added the protection then did the save as and each time I open the docx, the protection is still forms only instead of read only.
Ideally I would really like to know how to successfully delete the activedocument so I can save a pdf copy to the users desktop and then delete the Word doc.