Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook; user environment differences 1

Status
Not open for further replies.

fredmartinmaine

IS-IT--Management
Mar 23, 2015
79
0
0
US
The script below opens the Outlook default Inbox of the logged-in user. It displays the number of emails in the inbox, then interates through the first 4 emails, displaying sender and subject.

I log in as UserOne. The script is in a shared folder. When I run the script, it works exactly as expected.

Then, I log on as UserTwo. The script fails at assigning the sender and subject variables:
Object doesn't support this property method: 'objEmailItem.SenderEmailAddress'

Same machine, same code, same Outlook install. Different users. Clearly something different in the user environment, or registry, or something similar. It's beyond me what it might be. Does objEmailItem and objOutlook come from objNameSpace? Is that's what is different between the two? Outlook was already on the machine when I logged on as each (domain) user for the first time, creating these two fresh local profiles. What could be different?

Users are the same in terms of security. Both are common domain users but in the local admin group on this machine.

I need to find out how these two users can both run this script consistently without crashing.

------------------------------
Const olFolderInbox = 6
CR = chr(13)

Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)

MsgBox("Total Items: " & objFolder.Items.Count & CR & "Total Unread items: " & objFolder.UnReadItemCount)

count = 0

For Each objEmailItem In objFolder.Items

EmailAddrStr = objEmailItem.SenderEmailAddress
EmailSenderStr = objEmailItem.Sender
EmailSubjectStr = objEmailItem.Subject

count = count + 1

MsgBox(Count & " " & EmailAddrStr & CR & EmailSenderStr & CR & EmailSubjectStr & CR)

If count = 4 Then
Exit For​
End If​
Next

objOutlook.Quit

Set objEmailItem = Nothing
Set objFolder = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
 
Microsoft doc says:
Use GetNameSpace ("MAPI") to return the Outlook NameSpace object from the Application object.
In the script above, that's Set objOutlook =
Not sure how would that vary by user.
 
The problem is almost certainly that objEmailItem is not actually an email item, but rather one of the various Items that Outlook uses. One way of handling that might be to put a check into your loop, eg:

Code:
[blue]    For Each objEmailItem In objFolder.Items
        [b][COLOR=#A40000]If objEmailItem.Class = 43 Then[/color][/b] [COLOR=green]'Const olMail = 43[/color]
            EmailAddrStr = objEmailItem.SenderEmailAddress
            EmailSenderStr = objEmailItem.Sender
            EmailSubjectStr = objEmailItem.Subject

            Count = Count + 1

            MsgBox (Count & " " & EmailAddrStr & CR & EmailSenderStr & CR & EmailSubjectStr & CR)

            If Count = 4 Then
                Exit For
            End If
       [b] [COLOR=#A40000]Else
            [COLOR=green]' If you want to do anything with non-email items ...[/color][b][b][/b][/b]
        End If[/color][/b]
    Next[/blue]

 
Thanks, that's an eye opener for me. As it turns out, you've pointed out something I had not considered - for the user that's not having the trouble - that inbox was formerly an Exchange public folder and yes, also has PDFs and Word docs in it. For working user, only email. I will look at this on Monday.
 
>only email

there are items that superficially appear to be Mailitems, but are not, eg non-delivery reports, which are typically classified as ReportItems. You can see what is tripping the affected user up with another very simple modification that I left room for in my example code:

Code:
[blue]    For Each objEmailItem In objFolder.Items
        If objEmailItem.Class = 43 Then [COLOR=green]' Const olMail = 43[/color]
            EmailAddrStr = objEmailItem.SenderEmailAddress
            EmailSenderStr = objEmailItem.Sender
            EmailSubjectStr = objEmailItem.Subject

            Count = Count + 1

            MsgBox (Count & " " & EmailAddrStr & CR & EmailSenderStr & CR & EmailSubjectStr & CR)

            If Count = 4 Then
                Exit For
            End If
        Else
            [COLOR=green]' If you want to do anything with non-email items …[/color]
            [b][COLOR=#A40000]MsgBox "Non-mailitem - " & objEmailItem.Class & ": " & TypeName(objEmailItem)[/color][/b]
        End If
    Next [/blue]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top