如何使用MS Outlook中的Userform填充新电子邮件窗口(主题和正文字段)?

时间:2016-08-03 03:27:53

标签: vba email outlook-vba

我经常向供应商发出采购订单,以及我的工作;

  • 像普通人一样写电子邮件或<; li>
  • 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
    

我的目标是通过创建用户表单来消除错误的可能性,然后填充新的电子邮件窗口,允许我进行任何所需的更改,并添加附件,然后再手动单击[发送]。

Included here is a Userform I have created.

enter image description here

我创建的Userform中的文本框如下;

  1. RECIPIENTNAME
  2. PurchaseOrderNumber
  3. PurchaseOrderDescription
  4. 位置
  5. ProjectNumber
  6. DateRequired
  7. 在[SubmitButton]之后,数据将被填充到New E-mail窗口的Subject和Body字段中。

    主题行:

    • &#34; PO#[PurchaseOrderNumber] - [PurchaseOrderDescription] - [Location]&#34;

    体:

    • &#34;到[Recipient_Name], 请在[Location]中找到与[PurchaseOrderDescription]相关的附加采购订单(PO#[PurchaseOrderNumber])。所需日期:[DateRequired] 谢谢和亲切的问候,[用户的Outlook签名]&#34;

    我开发的代码如下;

    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
    

    目前,按下提交时,没有发生。 需要添加什么才能使其至少打开新电子邮件窗口?

1 个答案:

答案 0 :(得分:0)

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