Search code examples
outlookvbscript

VB Script called from Javascript - can't get Outlook Sender address


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

Solution

  • 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.