Vba 在新邮件中输入收件人时添加问候语

Vba 在新邮件中输入收件人时添加问候语,vba,email,outlook,Vba,Email,Outlook,我想在新邮件上自动创建带有收件人名字的问候语 例如,它应该检查“收件人”字段,如果有电子邮件地址,则从联系人中取名字并填写邮件正文(例如,亲爱的[名字],) 如果我按“新建邮件”,我不知道该使用哪种方法或事件,也不知道该使用哪种方法或事件来查看是否添加了收件人。(每次添加收件人时都应运行宏。)如果有两个以上的收件人,则应为“Hello everyone” 对于回复,我使用用户选择回复操作时发生的“回复”事件 Private Sub GMailItem_Reply(ByVal Response A

我想在新邮件上自动创建带有收件人名字的问候语

例如,它应该检查“收件人”字段,如果有电子邮件地址,则从联系人中取名字并填写邮件正文(例如,亲爱的[名字],

如果我按“新建邮件”,我不知道该使用哪种方法或事件,也不知道该使用哪种方法或事件来查看是否添加了收件人。(每次添加收件人时都应运行宏。)如果有两个以上的收件人,则应为“Hello everyone”

对于回复,我使用用户选择回复操作时发生的“回复”事件

Private Sub GMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
    AutoAddGreetingToReply Response
End Sub
这将查找收件人姓名并向回复邮件添加问候语

我还尝试了一个Word文档,其中包含要进行邮件合并的合并字段,但它不起作用。下面是我用于电子邮件合并的代码

Option Explicit
Public Sub MailMergeAttachments()
    Dim Session As Outlook.NameSpace
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    Dim oContact As ContactItem
    Dim oMail As MailItem
    Dim attach As Attachment
    Dim obj As Object
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim tmp As String
    ' Uses current user's profile
    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))

    ' Get Word
    Set oWord = GetObject(, "Word.Application")

Set oDoc = oWord.Documents(1)
 tmp = oDoc.FullName
 oDoc.Activate

oWord.Visible = True

    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection

  If Not TypeOf Selection.Item(1) Is Outlook.ContactItem Then
  MsgBox "You need to select Contacts first!"
  Exit Sub
  End If

For Each obj In Selection

'Test for ContactGroups
 If TypeName(obj) = "ContactItem" Then
 Set oContact = obj

Dim mText As String
Dim f As Word.Field

For Each f In oDoc.Fields

  If f.Type = wdFieldMergeField Then

 ' match Word mergefields with Outlook fields
      Select Case f.Code
       Case " MERGEFIELD First "
       mText = oContact.FirstName

       Case " MERGEFIELD Last "
       mText = oContact.LastName

       Case " MERGEFIELD Company "
       mText = oContact.CompanyName

       End Select

     f.Result.Text = mText
 End If
Next

Set oMail = Application.CreateItem(olMailItem)

With oMail
    .To = oContact.Email1Address
    .Subject = Left(oDoc.Name, Len(oDoc.Name) - 5)
    'The content of the document is used as the body for the email
    .Body = oDoc.Content
    .Attachments.Add enviro & "\Documents\instructions.pdf"
    .Display ' .send
End With

 End If
    Next
    Set oWord = Nothing
    Set Session = Nothing
    Set currentExplorer = Nothing
    Set obj = Nothing
    Set Selection = Nothing

End Sub

选择新邮件时如何运行宏,以及在输入新收件人并在邮件正文中添加个性化问候语时如何重复运行宏?

我有回复电子邮件的代码,这段代码将获得发件人的名字Dim olItem作为Outlook.MailItem,nameExample作为字符串nameExample=Split(olItem.SenderName,Chr(32))(0)我也有用于回复电子邮件的代码。我的问题是,如果我想写新消息并点击新消息,我真的不知道如何激活Sub()?