Modify Existing VBA Code

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,171
Office Version
  1. 365
Platform
  1. Windows
Hi,

The following VBA code works well for the most part but just need slight changes to it and I don't know how to make them. The code basically extracts the field Email, Name, and Phone Number from Outlook GAL based on a defined name range with a list of alias names.

Example:
Alias (column A)……….Column B...……………………...Column C...………..Column D
STYPEGHE ………………..joe.smith@outlook.com.... smith, joe...…….914-270-0010

Changes needed:

1) Code is returning a # N/A for the three extracted fields/columns from row 65,0001 to the last row of the worksheet. This should be blank since there is no alias listed (in the adjacent column A)
2) Add a header for each column of the three fields such as Email, Name and Phone Number.
3) Some emails comes like joe.smith@outlook.com (there is always a period between the first and last name) and would like to capitalize the "J"and "S", some are already come in correct form so those shouldn't change.

Thanks in advance!

VBA Code:
Option Explicit

Sub GetExchangeUserDetailsFromAlias()
  
    Dim str As String
    Dim olApp As Object 'Outlook.Application
    Dim olNameSpace As Object 'Outlook.Namespace
    Dim olRecipient As Object 'Outlook.Recipient
    Dim oEU As Object 'Outlook.ExchangeUser
    Dim arrUsers(1 To 65000, 1 To 3) As String
    Dim lngUser As Long
    Dim rngAlias As Range, rngAliasList As Range
  
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
  
    Set olNameSpace = olApp.GetNamespace("MAPI")
  
    With Worksheets("Users")
        Set rngAliasList = .Range("rngAliasList")
    End With
    For Each rngAlias In rngAliasList
        lngUser = lngUser + 1
        If Len(rngAlias.Value) > 0 Then
            str = rngAlias.Value
            Set olRecipient = olNameSpace.CreateRecipient(str)
            olRecipient.Resolve
            If olRecipient.Resolved Then
                If olRecipient.AddressEntry.AddressEntryUserType = 0 Then 'olExchangeUserAddressEntry
                    Set oEU = olRecipient.AddressEntry.GetExchangeUser
                    If Not (oEU Is Nothing) Then
                        With oEU
                            arrUsers(lngUser, 1) = .PrimarySmtpAddress
                            arrUsers(lngUser, 2) = .Name
                            arrUsers(lngUser, 3) = .MobileTelephoneNumber
                        End With
                    End If
                End If
            End If
        End If
    Next rngAlias
    rngAliasList.Offset(, 1).Resize(, 3).Value = arrUsers
  
    Set olApp = Nothing 'Outlook.Application
    Set olNameSpace = Nothing 'Outlook.Namespace
    Set olRecipient = Nothing 'Outlook.Recipient
    Set oEU = Nothing 'Outlook.ExchangeUser
    If lngUser Then Erase arrUsers
    Set rngAlias = Nothing
    Set rngAliasList = Nothing
  
End Sub
 
Everything looks good. The code runs and returns the data in 3 secs, I think my original was like 10 secs.
That's great. 10 seconds is still a lot quicker than I thought it would take, but I'm happy that we've managed to cut it down from 10 to 3 anyway.

That FUNCTION part of your code wasn't running so I just copied only that portion from the post above and it worked.
When you say that the function part of my code wasn't running, what do you mean? The ConvertCase function?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I suspected that might be the case. When Outlook can't be certain as to which alias you're referencing (and STYPEGHE is similar enough to STYPEGHE1), Outlook will just avoid the problem and do nothing. As far as I'm aware, there is only one way of resolving this - that's to use the Redemption library (link). Redemption allows you to work with properties and functionality not exposed through the Outlook Object Model. The developer version is free, but it requires installation, and so if you're doing this on a work issued laptop, it will require a conversation with the IT department before you could use it. There is probably a clever hack around it, though, but nothing comes immediately to mind. Maybe if you tried to resolve against an email address rather than an alias... I'd need to think about it.

How would I know I don't have access to RDO objects library?

Did you see this?:

"To use Redemption in a script or VBA, register redemption.dll (or redemption64.dll in case of a 64 bit version of Outlook) with regsvr32.exe in any folder. You can then create various Redemption objects using the CreateObject function (e.g. set session = CreateObject("Redemption.RDOSession"))."

Is it something in the code that you can modify to use RDO library objects?

P.S. your code in post #7 works fine, I think it was late last night and I maybe did something that made it not run the FUNCTION part of the code.
 
Upvote 0
What does this part of your code say? If I want to move the defined name alias to column B instead of A, what do I need to change here? Obviously the other part of the code with B1:D1 reference will change to C1:E1

VBA Code:
ReDim arrUsers(1 To lngLastRow - 1, 1 To 3)
 
Upvote 0
You can see whether or not it is installed by seeing if it is listed in References (click menu Tools -> References): "Redemption Outlook and MAPI COM library". I wonder if there might be a way around it though - for these aliases that have the additional 1 suffix, is it the case that both aliases refer to the same person, and that this person has changed his/her name? If so, is it easy enough to work out from the alias (roughly) what the email address is likely to be?
 
Upvote 0
You can see whether or not it is installed by seeing if it is listed in References (click menu Tools -> References): "Redemption Outlook and MAPI COM library". I wonder if there might be a way around it though - for these aliases that have the additional 1 suffix, is it the case that both aliases refer to the same person, and that this person has changed his/her name? If so, is it easy enough to work out from the alias (roughly) what the email address is likely to be?

Nope, don't see "Redemption Outlook and MAPI COM library" the only thing close to it is the "Microsoft Outlook 16.0 Object Library"

Those similar aliases are not the same person, completely different individuals. No worries though, it's just like 3 out of the 80 people from my current list. Can you reply to my post #13 question. Thanks!
 
Upvote 0
What does this part of your code say? If I want to move the defined name alias to column B instead of A, what do I need to change here? Obviously the other part of the code with B1:D1 reference will change to C1:E1

VBA Code:
ReDim arrUsers(1 To lngLastRow - 1, 1 To 3)
Ahh yes, sorry, I forgot to respond to this question. This line is not relevant to the point about where you want to output your results to; instead, it is used to resize and adjust the dimensions of the array that is being used to store the results. I'll give some thought to how you can exercise control over where on the worksheet the data will be output to, and see what I can do when I get home tonight.
 
Upvote 0
Hi - apologies for the delay - am taking the week off work, so finally have time to look at this. Looking back at your original code, it seems that the named range - rngAliasList - has been preset (which you later confirmed was range A:A). Because this was causing difficulties in situating the alias-related data and the column headers, I rewrote the code to limit the range - rngAliaslist - only to those aliases you put down (this range is different to the named range - you can disregard this for now). A quick and nasty way of repositioning the table is to change the relevant Range references below:

VBA Code:
With Worksheets("Users")
        lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row      '      <- CHANGE THE 'A' TO THE PREFERRED COLUMN
        Set rngAliasList = .Range("A2:A" & lngLastRow)          '         <- CHANGE THE 'A's TO THE PREFERRED COLUMN
End With
ReDim arrUsers(1 To lngLastRow - 1, 1 To 3)
   
Range("B1:D1").value = Array("Email", "Name", "Phone Number")          '<= CHANGE THE 'B' AND THE 'D' TO THE RESPECTIVE COLUMNS
                                                                       'IF CHANGING A TO B, THEN B = C AND D = E

With those changes, it should work. Alternatively, if you think it would be more helpful, we could you an InputBox which would prompt the user each time to select the column where they would like the data situated. Let me know how/if it works.
 
Upvote 0
HI,

I modified the code to lookup emails instead of aliases, didn't have to do much changes, and it worked fine, till i needed t get manager name and email (i highlighted where I am getting an error)

I only the thread is a bit old, but its the most relevant to what i need, so i hope someone can help

VBA Code:
[TABLE]
[TR]
[TD]Sub GetExchangeUserDetailsFromAlias()

    Dim str As String
    Dim olApp As Object 'Outlook.Application
    Dim olNameSpace As Object 'Outlook.Namespace
    Dim olRecipient As Object 'Outlook.Recipient
    Dim oEU As Object 'Outlook.ExchangeUser
    Dim arrUsers() As String
    Dim lngUser As Long
    Dim rngAlias As Range, rngAliasList As Range
    Dim lngLastRow As Long

    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0

    Set olNameSpace = olApp.GetNamespace("MAPI")

    With Worksheets("Unique missing from 2020")
        lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        Set rngAliasList = .Range("A2:A" & lngLastRow)
    End With
    ReDim arrUsers(1 To lngLastRow - 1, 1 To 5)

    With Range("B1:F1")
        .Value = Array("Name", "Department", "Job Title", "Manager", "Manager Email")
    End With
    
 
    For Each rngAlias In rngAliasList
        lngUser = lngUser + 1
        If Len(rngAlias.Value) > 0 Then
            str = rngAlias.Value
            Set olRecipient = olNameSpace.CreateRecipient(str)
            olRecipient.Resolve
            If olRecipient.Resolved Then
                    Set oEU = olRecipient.AddressEntry.GetExchangeUser
                    If Not (oEU Is Nothing) Then
                        With oEU
                            arrUsers(lngUser, 1) = .Name
                            arrUsers(lngUser, 2) = .department
                            arrUsers(lngUser, 3) = .JobTitle
[COLOR=rgb(226, 80, 65)]                            arrUsers(lngUser, 4) = .GetExchangeUserManager.Name
                            arrUsers(lngUser, 5) = .GetExchangeUserManager.PrimarySmtpAddress[/COLOR]
                        End With
                    End If
            End If
        End If
    Next rngAlias
    rngAliasList.Offset(, 1).Resize(, 5).Value = arrUsers
        
    Worksheets("Unique missing from 2020").Columns("A:F").EntireColumn.AutoFit

    Set olApp = Nothing 'Outlook.Application
    Set olNameSpace = Nothing 'Outlook.Namespace
    Set olRecipient = Nothing 'Outlook.Recipient
    Set oEU = Nothing 'Outlook.ExchangeUser
    If lngUser Then Erase arrUsers
    Set rngAlias = Nothing
    Set rngAliasList = Nothing

End Sub
[/TD]
[/TR]
[/TABLE]

Thanks
 
Upvote 0
HI Qadah - Thanks for visiting the Mr Excel Forum. I'm not a moderator, (so I can't do this for you) but I think it would be more helpful for you if you began a new thread for your question above. By all means, you should provide a link to this original thread along with your explanation above so that people can understand how you got to where you are now, but as it is, I think I may be the only one who will have seen you've asked this question and that was because I was involved in the original thread. When you re-ask your question above as a new thread, it will come to the attention of more people (more intelligent than me!) and you will likely get quicker help that way. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,783
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top