Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Automatically add greetings when sending Outlook emails

$
0
0

I need to write a code that automatically creates a greeting with the recipients first name. For example, it should check the "To" field and if there is an email, take the First Name from the contact and fill authomatically in the body of the message (ex. Dear [First Name],).

First of all, I don't really know which method or event should I use if I press New Message and which method or event should I use to see if a recipient is added (and the macro should run everytime when a new recepient is added). If there are more then 2 recipients should be "Hello everyone,"

For replay on existing email I am using the "Reply" event that occures when the user selects the reply action. For example:

Private Sub GMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
    AutoAddGreetingToReply Response
End Sub

This helps me to find easily the recepient name and add greeting in the replay message. But actually I don't really need only on replying messages. I need it when I create a new message :/

I also tried with a Word document that contains merge fields to mail merge but it does't really work. Here is the code that I am using for email merge.

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

So, the Question is: How to run a macro when I selects new message and how to run it repeatedly everytime when I enter new recipient/s and add personalised greeting in the body of the message?


Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>