Link to home
Start Free TrialLog in
Avatar of Adam Ehrenworth
Adam EhrenworthFlag for United States of America

asked on

VBA - Get EntryID of Outlook Message that is Open (versus selected)

I have the following piece of code that is getting the EntryID of an Outlook message that is selected. However, I want to have a button to execute this script on the custom toolbar of the message itself,

How I get the Entry ID of the currently "open" message ID versus a highlighted one?

Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection
    
    For Each currentItem In Selection
        If currentItem.Class = olMail Then
            Set currentMail = currentItem
        End If
    Next
        
    strEntryID = currentMail.entryID

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
outlook
ALT f11
insert this in a module for macros
open a message (it will not open it for you to install the toolbar, but you can add it, or i can)
run the part that install the toolbar and button (test sub)
NOTE: this is an old method to install buttons, normally you would generate a xml file with a new ribbon
click the button inside the message

Tested in outlook 2013

Sub test()
    
    Dim objbarre As Object
    toolbarname01 = "toolbar01"
    barispresent01 = 0
    buttonaction01 = "getid"
    buttonname01 = "Get ID"
    
    Set toolbars01 = ActiveInspector.CommandBars
    For Each toolbar01 In toolbars01
        If toolbar01.Name = toolbarname01 Then
            'toolbar01.Delete
            barispresent01 = 1
            Set toolbar02 = toolbar01
        End If
    Next
    
    If barispresent01 = 1 Then
        Set toolbar01 = toolbar02
    End If
    
    Set objbarre = Nothing
    
    If barispresent01 = 0 Then
        Set toolbar01 = ActiveInspector.CommandBars.Add(toolbarname01)
    End If
    
    '=== rend barre visible
    If Not toolbar01 Is Nothing Then
        toolbar01.Name = toolbarname01
        toolbar01.Visible = True
        
        If barispresent01 = 0 Then toolbar01.Position = msoBarTop
        
        For Each objcontrol In toolbar01.Controls
            '=== delete button, but not the bar, so it stay in place
            objcontrol.Delete
        Next
        MsgBox (toolbar01.Name)
        
        Set button01 = ActiveInspector.CommandBars(toolbarname01).Controls.Add(Type:=msoControlButton, Before:=1)
        
        With button01
            'buttons(depnum, 0)
            .Caption = buttonname01
            .TooltipText = "Get ID of opened message"
            .Enabled = True
            .Visible = True
            .OnAction = "getid01"
            .Tag = buttonaction01
            .Style = msoButtonIconAndCaption
            .FaceId = 5432
        End With
    Else
        MsgBox ("ERROR tool bar not created")
        
        
    End If
End Sub
Sub getid01()
    
    Set currentMail = Application.ActiveInspector.CurrentItem
    
    strentryid = currentMail.EntryID
    
    MsgBox ("strentryid: " & strentryid)
    
    
    
End Sub

Open in new window

Avatar of Adam Ehrenworth

ASKER

Was able to just use this code on the main screen versus on the open mail.