PDA

View Full Version : Assistance with sending document to a specified email address



tb3
10-20-2014, 06:17 AM
Hello and thankyou for your time.

I am using Word and Outlook 2010. I am trying to create a form that includes a button at the bottom titled "Submit", which when clicked, will attach the form to an email and place a specified email address in the To field.

I am new to VBA and don't have much experience. I have been able to get the following coding to open the email dialogue box and attach the form but can't work out how to get the specific email address to be included automatically.


Private Sub CommandButton1_Click()
Options.SendMailAttach = True
ActiveDocument.SendMail


End Sub




Any help greatly appreciated.

Thankyou

gmayor
10-20-2014, 06:52 AM
You need a little more than that if you want to process the message :) The following should do the trick. Reinstate the .Send command when you are happy with the results.
The macro doesn't work if there is no Outlook available!


Private Sub CommandButton1_Click()
' send the document as an attachment _
in an Outlook Email message
Dim bStarted As Boolean
Dim oOutlookApp As Object
Dim olInsp As Object
Dim oItem As Object
Dim wdDoc As Object
Dim oRng As Object

On Error Resume Next

'Prompt the user to save the document
ActiveDocument.Save

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
On Error GoTo 0

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(0)

With oItem
.to = "someone@somewhere.com"
.Subject = "This is the subject"
.Attachments.Add ActiveDocument.FullName
.BodyFormat = 2 'olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
.Display
'Move the cursor to the start of the message
oRng.Collapse 1
'And insert the message body text
oRng.Text = "This is the first line of the message" _
& vbCr & "This is the next line of the message"
'etc
'.send 'Only include this line after testing
End With

Set oItem = Nothing
Set oOutlookApp = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
lbl_Exit:
Exit Sub
End Sub


When your forms are returned, you might find http://www.gmayor.com/extract_email_data_addin.htm useful

ranman256
10-20-2014, 07:00 AM
Sub SendMyDoc()
Options.SendMailAttach = True
ActiveWindow.EnvelopeVisible = True
With ActiveDocument.MailEnvelope.Item
.Subject = "eMAIL Testing 123"
.Recipients.Add "myAddr@abc.com"
.send
End With
End Sub