0
votes

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.

1

1 Answers

0
votes

This code will do the following:

  • Open new instance of Word
  • Copy contents of macro enable doc to the new instance of word
  • save the new instance doc in the temp location
  • you need to then add the code for emailing etc
  • it will then delete the new instance word doc from the temp location
  • close the new instance of word
  • close the macro enables word doc

See Code below:

    Option Explicit

    Sub SaveAndDeleteBeforeClosing()

        'Tested and working well with Office 2010

        Dim FormDocument As Document
        'Activedocument can be changed to reference a specific document name.
        'Set FormDocument = Documents("Some Doc Name")
        Set FormDocument = ActiveDocument

        'Opening new instance of Word
        Dim WordProg As Word.Application
        Set WordProg = CreateObject("Word.Application")
        WordProg.Application.Visible = False

        'Adding a new document toi the new instance of Word
        WordProg.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
        Dim WordDoc As Document
        Set WordDoc = WordProg.Documents(WordProg.Documents.Count)

        'Copy contents of Macro Document to new document
        FormDocument.Content.Copy
        WordDoc.Range.Paste

        'Use this to as a sanity check to see if everything is copied over correctly
        'otherwsie you will need to play around with the paste options
        WordProg.Visible = True

        'Saving New instance word doc to temp location

        Dim FileNameString As String
        'Enter your desired file name here
        FileNameString = "Some Doc Name"
        Dim FilePathString As String
        'Temp file for deleting
        FilePathString = "C:\Temp\" & FileNameString & ".docx"

        WordDoc.SaveAs2 FileName:=FilePathString, FileFormat:= _
            wdFormatDocumentDefault, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=14

        'Closing new instance word document
        WordDoc.Close

        'Some code to send the file as a email
        '
        '
        '
        '
        '
        'delete the file from the C:\Temp\ folder
        Kill (FilePathString)

        'Quitting the temp word application
        WordProg.Application.Quit
        'Quitting the word application inwhich the macro is stored
        Application.Quit False

    End Sub