PDA

View Full Version : Macro that loads a custom form and creates the subject from select body text



blakem
09-06-2016, 09:14 PM
Hi,
Title; Macro that loads custom form, creates subject line from input and adds to body.
I'm using outlook 2010.

I'm trying to setup a macro that when you run it, it loads a dialog box which acts you for some text to input, then it takes that text and places it in the subject line and also places it in the body of the e-mail.

Any help would be greatly appreciated as i'm new to VBA with outlook.

Regards,

Blake

gmayor
09-06-2016, 09:38 PM
What you ask is fairly straightforward, but you will need to post your custom form template and indicate where you want the data from the userform to be placed in the body of the form.

blakem
09-06-2016, 09:51 PM
Thanks for the response.
I am looking to have an invoice number put into the subject line and have that also placed in the body indicated by the brackets.
17037

gmayor
09-06-2016, 10:42 PM
As this appears to relate to an invoice, what format is the invoice in? It might be possible to simply read the name and number from the invoice and attach the invoice to the message.
If that is possible then that would make more sense than prompting for the name and number. However you can use input boxes (as below) or a userfrorm http://www.gmayor.com/Userform.htm to prompt for the data:

Sub InvoiceMessage()
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim strName As String
Dim strInvoice As String
Dim strText As String
strName = InputBox("Enter Name:")
strInvoice = InputBox("Invoice Number:")
strText = "Dear " & strName & vbCr & vbCr & _
"Please find attached invoice number " & _
strInvoice & Chr(46) & vbCr
On Error Resume Next
Set olEmail = CreateItem(olMailItem)
With olEmail
.BodyFormat = olFormatHTML
.subject = "Invoice Number " & strInvoice
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = strText
oRng.collapse 0
.Display
End With
lbl_Exit:
Set olEmail = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

blakem
09-11-2016, 04:06 PM
Thanks so much for your help! It works perfectly. (Sorry for the late reply I've been out of the office)