VBA code to email doc...NEED HELP!

megha

New Member
Joined
May 15, 2009
Messages
46
I am using the following code to email word document (its actually a form) as attachment and to save it to my “P:/” drive. It seems working but the code will need to modify for the following reasons:

1) Once I am done filling out the form and hit the command button to run the macro… the document (form) actually sent to email and also saved to my “P:/” drive successfully but its over saved the master file. I need the form to be blank at all time.
2) Also, I want to have the files to be stamp with date and time (as a file name for the form) that going to be email and save to “p:/” drive.

I will appreciate any help. Thanks!!

The code is:

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.SaveAs
With EmailItem
.Subject = "Insert Subject Here"
.Body = "Insert message here" & vbCrLf & _
"Line 2" & vbCrLf & _
"Line 3"
.To = "name@company.com"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.FullName
.Send
End With

Application.ScreenUpdating = True

Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing

Flag = True
sPath = "P:\"
ActiveDocument.SaveAs FileName:=sPath & ActiveDocument.Name
Flag = False
Application.Documents.Close
CommandButton1.Visible = False

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,225,521
Messages
6,185,461
Members
453,294
Latest member
mpfred6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top