Outlook code need to get just the Firstname or Alias. To the body of the mail.

bsharath
bsharath used Ask the Experts™
on
Hi,

Outlook code need to get just the Firstname or Alias. To the body of the mail.
The code is from David. it works fine but need the Alias or first name into the body next to hi
Like
Hi Firstname,
Or
Hi Alias,

Regards
Sharath

Sub PredefinedResponse()
    Dim olkItem As Outlook.MailItem, olkResponse As Outlook.MailItem, olkAddressee As Outlook.Recipient
    For Each olkItem In Application.ActiveExplorer.Selection
        For Each olkAddressee In olkItem.Recipients
            If olkAddressee.Type = olTo Then
                Set olkResponse = Application.CreateItem(olMailItem)
                With olkResponse
                    .Recipients.Add olkAddressee.Address
                    'Edit the message subject on the next line.'
                    .Subject = "Happy Birthday"
                    'Edit the message body on the next line.'
                    .HTMLBody = "Hi " & olkAddressee.Name & ",<br><br><img src=""D:\MyImage.gif"" /><br><br>Regards<br>Sharath"
                    .Display
                End With
            End If
        Next
    Next
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
Hi, Sharath.

The problem is that the name is not broken down into first name, last name, etc. and there is no standard format.  Sometimes it will be first last other times it'll be last, first.  This splits the name based on spaces and then uses the left-most name.  Using my name as an example, if it represented as David Lee, then it will get David.  If it is represented as Lee, David, then it will get Lee.
Sub PredefinedResponse()
    Dim olkItem As Outlook.MailItem, olkResponse As Outlook.MailItem, olkAddressee As Outlook.Recipient, strName As String
    For Each olkItem In Application.ActiveExplorer.Selection
        For Each olkAddressee In olkItem.Recipients
            If olkAddressee.Type = olTo Then
                Set olkResponse = Application.CreateItem(olMailItem)
                With olkResponse
                    .Recipients.Add olkAddressee.Address
                    'Edit the message subject on the next line.'
                    .Subject = "Happy Birthday"
                    arrName = Split(olkAddressee.Name, " ")
                    strName = Replace(arrName(0), ",", "")
                    'Edit the message body on the next line.'
                    .HTMLBody = "Hi " & strName & ",<br><br><img src=""D:\MyImage.gif"" /><br><br>Regards<br>Sharath"
                    .Display
                End With
            End If
        Next
    Next
End Sub

Open in new window

Top Expert 2010

Commented:
If all of the senders that you will ever use this with are in your organization, and if your organization is using Active Directory, then I can write a function to use their name to find their account in AD and get their first name, or last name, or whatever name you want to use so long as it is stored in AD.

Author

Commented:
The code what it does not spliting from the space is perfect
How can i get the name resolved.?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2010

Commented:
"The code what it does not spliting from the space is perfect"
Sorry, I've no idea what you mean.

"How can i get the name resolved.?"
The name is resolved.  The issue is what Outlook has for a name and how it is formatted.  If the name is an outside name (someone not in your organization), then there is nothing you can do except split the name like I'm doing.  If the name is ina inside name (someone who is in your organization), then as I said above we can look them up in AD (assuming of course that you have AD).

Author

Commented:
Sorry typo
its perfect i just want to resolve the id's
I use this code only for AD users

I just checked and placed this
.Recipients.ResolveAll
and it works perfect now
thank U
Top Expert 2010

Commented:
You're welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial