Code for clearing form fields after emailing filled-out form

344 views Asked by At

I have created a 2010 Word doc with form fields and a command button to email the form. I did find code to close the doc after the email sent but, I need it to not save the changes before it closes the doc or clears the fields and uncheck the check boxes before it closes.

The goal is that they open a fresh form every time.

They are a contract company as well so I'm sending the form for them to save to their own drive, other wise I would have saved to our templates.

Private Sub CommandButton1_Click() 
    Dim OL As Object 
    Dim EmailItem As Object 
    Dim Doc As Document 
    Application.ScreenUpdating = False 
    Set OL = CreateObject("Outlook.Application") 
    Set EmailItem = OL.CreateItem(olMailItem) 
    Set Doc = ActiveDocument 
    Doc.Save 
    With EmailItem 
        .Subject = "Medical/Psych router" 
        .To = "[email protected]" 
        .Importance = olImportanceNormal 
        .Attachments.Add Doc.FullName 
        .Send 
    End With 
    Application.ScreenUpdating = True 
    Set Doc = Nothing 
    Set OL = Nothing 
    Set EmailItem = Nothing 
    Application.ActiveDocument.Close
End Sub
1

There are 1 answers

2
0m3r On

Try saving it to Temp Folder C:\Temp\, The Master Doc will be blank and you will have new file saved at C\Temp\ with file name Medical_Psych_router today's date Jun_18_2015. (Medical_Psych_router Jun_18_2015)

Option Explicit
Private Sub CommandButton1_Click()
    Dim OL As Object
    Dim EmailItem As Object
    Dim Doc As Document
    Dim sFileName As String

    Application.ScreenUpdating = False

    Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(olMailItem)
    Set Doc = ActiveDocument
    sFileName = "Medical_Psych_router " & Format(Now, "mmm_dd_yyyy")
    Doc.SaveAs FileName:=("C:\Temp\") & sFileName, _
                                FileFormat:=wdFormatDocument, _
                                AddToRecentFiles:=True

        'Doc.Save
        With EmailItem
            .Subject = "Medical/Psych router"
            .To = "[email protected]"
            .Importance = olImportanceNormal
            .Attachments.Add Doc.FullName
            .Send
        End With

        Application.ScreenUpdating = True

        Set Doc = Nothing
        Set OL = Nothing
        Set EmailItem = Nothing
        Application.ActiveDocument.Close SaveChanges:=False

End Sub