I am using the following script that I found online (with a few minor changes to suit my specific needs) in Outlook to submit certain emails I receive to a helpdesk ticketing system. The problem with this script is that the "SenderEmailAddress" property of the Outlook.MailItem is actually pulling the "legacyExchangeDN" attribute from Active Directory, and that does not always contain the correct username (e.g., if an AD user got renamed at some point, for whatever reason that attribute never changes).
The email address of the user submitting the ticket should be stored in the "emailUser" string, which is being derived from objItem.SenderEmailAddress
and is sometimes wrong because of this problem.
My question should be pretty simple for you VB gurus... I need to modify this script so that "emailUser" is set to be whatever the user's primary e-mail address is. I'm guessing you can just query AD and find the user object whose name is equal to the Outlook.MailItem.Sender property... but alas, I do not know how to build that query.
Dim helpdeskaddress As String
Dim objMail As Outlook.MailItem
Dim strbody As String
Dim oldmsg As String
Dim emailUser As String
Dim objItem As Outlook.MailItem
' Set this variable as your helpdesk e-mail address
helpdeskaddress = "firstname.lastname@example.org"
Set objItem = GetCurrentItem()
Set objMail = objItem.Forward
'get the username portion of the sender email address by parsing LDAP string
If (InStr(1, objItem.SenderEmailAddress, "CN=") > 0) Then
emailUser = (Right(objItem.SenderEmailAddress, (Len(objItem.SenderEmailAddress) - 57))) & "@company.com"
emailUser = objItem.SenderEmailAddress
'adds the senders e-mail address as the created by object for the ticket and appends the message body
strbody = "#created by " & emailUser & vbNewLine & vbNewLine & objItem.Body
objMail.To = helpdeskaddress
objMail.Subject = objItem.Subject
objMail.Body = strbody
' remove the comment from below to display the message before sending
'Automatically Send the ticket
Set objItem = Nothing
Set objMail = Nothing
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Set GetCurrentItem = _
Set GetCurrentItem = _