0

I am calling a VBS function from an intranet page, using IE 11 (yes, it has to be that browser). The function processes an Outlook mailbox, and is partly working: I can run down the list of mails and find the Subject and Body, but I need the sender's email address. I can get this in various ways when I run similar code as an Outlook macro, but none of them work in VBS - the script just hangs (no error message) when I try to get anything of interest - see the function, with various things I've tried commented. Any pointers as to where I'm going wrong gratefully received....

    Function readEmails(mailbox)
    Dim iCt  
        Set objOutlook = CreateObject("Outlook.Application")
        Set NS = objOutlook.GetNamespace("MAPI")
        Set olFolder = NS.Folders(mailbox)
        Set olFolInbox = olFolder.Folders("Inbox")
    
        iCt = 0
        For iCt =1 to olFolInbox.Items.Count 
            set olMessage=olFolInbox.Items(iCt)
                msgbox(ict & "-" & olMessage.Subject )
                msgbox("SenderEmailType=" & olMessage.SenderEmailType)
                msgbox("SenderEmailAddress=" & olMessage.SenderEmailAddress)    'hangs
                'set sn=olMessage.SenderName 'hangs
                set sUser=olMessage.Sender
                'set sn=sUser.Name 'hangs
                'msgbox(sUser) 'hangs
                'set sExUser=sUser.GetExchangeUser ' hangs
            End If
        Next 
        readEmails=sReturn
    End Function
JasonC
  • 3
  • 1
  • You could try checking [`SenderEmailAddress`](https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.senderemailaddress) has a value using `Len(SenderEmailAddress & "") > 0` first. – user692942 Aug 17 '21 at 07:20
  • Thanks - I added msgbox("Len="&Len(olMessage.SenderEmailAddress & "")) - both with and without the & "" - and again it just hangs when it gets to that line, nothing is displayed at all – JasonC Aug 17 '21 at 09:28
  • The thing is the [`Items` collection](https://learn.microsoft.com/en-us/office/vba/api/outlook.folder.items) can deal with [multiple types of outlook objects](https://learn.microsoft.com/en-us/office/vba/outlook/how-to/items-folders-and-stores/outlook-item-objects), so you might need to first check you are dealing with a [`MailItem`](https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem) before attempting to read the property. See [Check for the senderEmailAddress](https://stackoverflow.com/a/42547062) for some VBA examples you can modify. – user692942 Aug 17 '21 at 13:29
  • 1
    Thanks, user692942, you make a valid point in general, but I happen to know I'm dealing with all MailItems here. The same code works fine in an Outlook macro, but not in VB Script called from an HTML/Javascript page. Anyway, I'll check out your examples and see if it gets me anywhere. – JasonC Aug 17 '21 at 14:47

1 Answers1

0

It seems that a security issue takes its place when you automate Outlook from an external macro - it can be a security prompt or an exception in the code. How it is seen really depends on the Outlook version (its internal implementation). To avoid security issues when dealing with OOM you can use the following approaches:

  1. Use a low-level API which doesn't trigger security issues in OOM. Outlook is built on top of Extended MAPI which doesn't trigger security issues unlike OOM. Also you may consider using any wrappers around this low-level API such as Redemption.
  2. Use third-party components designed for turning off and on security checks in OOM, see Outlook Security Manager for more information.
  3. You can create a group policy to prevent security prompts from displaying if any up-to-date antivirus software is installed on the system or just turn these warning off (which is not really recommended).

Users get the security prompts/exceptions because Outlook is configured on the client computer in one of the following ways:

  • Uses the default Outlook security settings (that is, no Group Policy set up)
  • Uses security settings defined by Group Policy but does not have programmatic access policy applied
  • Uses security settings defined by Group Policy which is set to warn when the antivirus software is inactive or out of date

Read more about that in the Security Behavior of the Outlook Object Model article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks, Eugene - I think you have found the problem. Given corporate restrictions, I am unlikely to be able to change the security settings, but I can think about achieving my aim in a different way. – JasonC Aug 18 '21 at 09:03