PDA

View Full Version : Email and attach macro



alsbc
02-14-2018, 01:49 AM
I have the below macro to execute a command that saves document to original location and sends an email using textfield content to determine subject

I need to change so that it does not save the document just attaches and that the file name is pulled from textbox1 field content

21618

gmayor
02-14-2018, 02:05 AM
You will have to save the document in order to attach it - even if you save a temporary copy of it and delete it afterwards,
What sort of field is textbox1?

alsbc
02-14-2018, 02:08 AM
just a text name field, could I set a default save location to auto save them to

gmayor
02-14-2018, 02:34 AM
Just a text name field?

ActiveX?
Legacy Form Field?
Content Control?
Userform TextBox?

You can save to the Temporary folder


Dim strPath as String
strPath = Environ("TEMP") & "\"

alsbc
02-14-2018, 02:54 AM
userform textbox

gmayor
02-14-2018, 04:43 AM
In that case something like


Private Sub Submit_Click()
'No-option email sending
Dim OL As Object
Dim EmailItem As Object
Dim strPath As String
Dim strName As String
Dim oDoc As Document

Application.ScreenUpdating = True
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
strPath = Environ("TEMP") & Chr(92)

If TextBox1.Text = "" Then GoTo lbl_Exit
strName = TextBox1.Text
If Not Right(strName, 5) = ".docx" Then strName = strName & ".docx"
Set oDoc = ActiveDocument
oDoc.SaveAs2 FileName:=strPath & strName, AddToRecentFiles:=False

With EmailItem
.Subject = TextBox1 & "OVERVIEW OF SUPPORT FORM SUBMITTED"
.Body = "Find attached Overview of support form." & vbCrLf & _
"" & vbCrLf & _
""
.to = "l2@bradfordcollege.ac.uk"
.Importance = 0
.Attachments.Add oDoc.FullName
.send
End With
oDoc.Close 0
Kill strPath & strName
Application.ScreenUpdating = True
lbl_Exit:
Unload Me
Set oDoc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
Exit Sub
End Sub

alsbc
02-14-2018, 05:16 AM
thanks for this, where would this save

gmayor
02-14-2018, 07:24 AM
It would save in the User's Temp folder - enter %temp% in the address windows of Windows File Explorer.
It would add the saved document to the message, send the message and then delete the saved document.

alsbc
02-14-2018, 08:27 AM
Fantastic, works a treat. Thanks for your prompt and useful assistance. :)