How to access certain mailitem properties/methods in a Visual Studio Outlook Add-In?

0

I'm developing an Outlook (2010) add-in using Visual Studio 2013 (Targeting .NET 4).

Certain Outlook properties/methods seem to be unavailable.

The following code works from Outlook VBA.

Public Sub OutlookTest()

    'Dim oApp As New Outlook.Application  (NOT NEEDED FOR OUTLOOK VBA)
    Dim oExp As Outlook.Explorer
    Dim oSel As Outlook.Selection   ' You need a selection object for getting the selection.
    Dim oItem As Object             ' You don't know the type yet.    
        
    Set oExp = Application.ActiveExplorer 'Get the ActiveExplorer.
    Set oSel = oExp.Selection           ' Get the selection.

    For i = 1 To oSel.Count         ' Loop through all the currently .selected items
        Set oItem = oSel.Item(i)        ' Get a selected item.
        Call DisplayInfo(oItem)          ' Display information about it.
    Next i

End Sub

Private Sub DisplayInfo(oItem As Object)

    Dim strMessageClass As String
    Dim oMailItem As Outlook.MailItem

    ' You need the message class to determine the type.
    strMessageClass = oItem.MessageClass

    If (strMessageClass = "IPM.Note") Then          ' Mail Entry.
        Set oMailItem = oItem
        MsgBox oMailItem.Subject
        MsgBox oMailItem.EntryID
        MsgBox oMailItem.HTMLBody
        oMailItem.SaveAs "C:\Users\u001tb7\Desktop\New folder\testOL.msg", olMSG
    Else
            MsgBox "Pick something else"
        End If

    End Sub

When I try near identical code from Visual Studio in an add-in:

Private Sub butSettings_Click(sender As Object, e As RibbonControlEventArgs) Handles butSettings.Click

    Dim oApp As New Outlook.Application
    Dim oExp As Outlook.Explorer
    Dim oSel As Outlook.Selection   ' You need a selection object for getting the selection.
    Dim oItem As Object             ' You don't know the type yet.

    oExp = oApp.ActiveExplorer      ' Get the ActiveExplorer.
    oSel = oExp.Selection           ' Get the selection.

    For i = 1 To oSel.Count         ' Loop through all the currently .selected items
        oItem = oSel.Item(i)        ' Get a selected item.
        DisplayInfo(oItem)          ' Display information about it.
    Next i

End Sub

Sub DisplayInfo(oItem As Object)

    Dim strMessageClass As String
    Dim oMailItem As Outlook.MailItem

    ' You need the message class to determine the type.
    strMessageClass = oItem.MessageClass

    If (strMessageClass = "IPM.Note") Then          ' Mail Entry.
        oMailItem = oItem
        MsgBox(oMailItem.Subject)
        MsgBox(oMailItem.EntryID)
        MsgBox(oMailItem.HTMLBody)      '<---FAILS
        oMailItem.SaveAs("C:\Users\u001tb7\Desktop\New folder\testVS.msg", Outlook.OlSaveAsType.olMSG)  '<---ALSO FAILS
    Else
        MsgBox("Pick something else")
    End If

End Sub

I get an error on MailItem.HTMLBody and MailItem.SaveAs but NOT MailItem.Subject or .EntryID.

This makes me suspect it's something to do with the security, as I think properties like .HTMLBody are 'protected', but .EntryID and .Subject are not.

The error is the generic COM exception not giving me any detail:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in MsgSave.dll but was not handled in user code

Additional information: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

Is there any way to get Outlook to 'trust' my VS code (for eventual distribution)? Or is there something else amiss?

EDIT: Thanks to both below!

Instead of:

Dim oApp As New Outlook.Application

Use:

Dim oApp as Outlook.Application = Globals.ThisAddIn.Application
vba
visual-studio-2013
outlook
outlook-addin
asked on Stack Overflow Feb 4, 2015 by BrINClHOF • edited Dec 20, 2020 by Community

2 Answers

2

Do not use New Outlook.Application in an Outlook addin - you get Outlook.Application object for free when your addin starts up.

1

certain outlook properties/methods seem to be unavailable

What properties are you talking about? Could you be more specific?

As Dmitry suggested, you need to use the Application property provided by VSTO. In that case you will avoid security prompts or exceptions. Typically you get such issue when you try to automate Outlook from a standalone application. The Application object provided by VSTO is trusted and doesn't generate exceptions for secured properties or methods (for example, MailItem.Send). You can read more about that in the Outlook "Object Model Guard" Security Issues for Developers article.

answered on Stack Overflow Feb 4, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0