简体   繁体   中英

How can I populate the New E-mail Window (Subject and Body fields) using a Userform in MS Outlook?

I regularly issue Purchase Orders to vendors for my job, and either;

  • Write the email like a normal person or;
  • Use a macro to open a template that I have generated
    Sub New_Email_from_PO_Template() Dim myOlApp As Outlook.Application Dim MyItem As Outlook.MailItem Set myOlApp = CreateObject("Outlook.Application") Set MyItem = myOlApp.CreateItemFromTemplate("C:\\Blah\\template.oft") MyItem.Display End Sub

My aim is to eliminate the possibility of error by creating a userform, which will then populate a New E-mail window, allowing me to make any desired changes, and add attachments, before manually clicking [Send].

Included here is a Userform I have created.

在此处输入图片说明

The textboxes in the Userform that I have created are as follows;

  1. RecipientName
  2. PurchaseOrderNumber
  3. PurchaseOrderDescription
  4. Location
  5. ProjectNumber
  6. DateRequired

Following [SubmitButton], the data would then be populated into the Subject and Body fields in the New E-mail window.

  • "PO# [PurchaseOrderNumber] - [PurchaseOrderDescription] - [Location]"

  • "To [Recipient_Name], Please find attached Purchase Order (PO# [PurchaseOrderNumber]) pertaining to [PurchaseOrderDescription] for [Location]. Date Required: [DateRequired] Thanks and Kind Regards, [User's Outlook Signature]"

The code I have developed is below;

Sub ShowPOSubmissionUserform()
POSubmissionEmailGenerator.Show
End Sub

Sub InitialisePOSubmissionUserform()
'Empty ProjectNumberTextbox
ProjectNumberTextbox.Value = ""

'Empty ProjectNameTextbox
ProjectNameTextbox.Value = ""

'Empty PONumberTextbox
PONumberTextbox.Value = ""

'Empty RecipientNameTextbox
RecipientNameTextbox.Value = ""

'Empty DateRequiredTextbox
DateRequiredTextbox.Value = ""
End Sub


Private Sub CloseButton_Click()
Unload Me
End Sub


Private Sub SubmitButton_Click()
'Creates a new e-mail item and modifies its properties
Dim OutlookApp As Object
Dim MItem As Object
Dim email_ As String
Dim subject_ As String
Dim body_ As String
Dim attach_ As String
Set OutlookApp = CreateObject("Outlook.Application")
email_ = POSubmissionEmailGenerator.ProjectNumberTextbox.Value
subject_ = "Hello this is the subject"
body_ = "Line 1" & vbNewLine & vbNewLine & "Line 3"
'create Mail Item and send it
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = email_
.Subject = subject_
.Body = body_
End With
End Sub

At the moment, when pressing submit, NOTHING happens. What needs to be added to make it at least open the New E-mail window?

With MItem
    .To = email_
    .Subject = subject_
    .Body = body_
    .Display '<<  
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM