Max
Max

Reputation: 759

How to open the folder of current open item and select that item?

I have a Sub that opens the Folder for the current open mail-item.

This makes sense if I have an item open, but have changed the mail-Folder inbetween, and want to open the right Folder straight away again.

Sub ordner_mail_oeffnen()
    On Error GoTo exit_sub
    'Dim olApp As Outlook.Application
    Dim olitem As Outlook.mailitem
    'Set olApp = Outlook.Application
    Set olitem = Outlook.Application.ActiveInspector.CurrentItem

    Dim olfolder As MAPIFolder
    Dim FolderPath As String
    Dim Subfolder As Outlook.MAPIFolder
    FolderPath = GetPath(olitem)
    Set olfolder = GetFolder(FolderPath)
    olfolder.Display

    'those two lines are just for test purpose
    MsgBox "jetzt"
    Application.ActiveExplorer.ClearSelection

    Sleep (10000)
    Application.ActiveExplorer.ClearSelection
    'here comes the runtime-error (I try to translate) "-2147467259 (80004005) element can not be activated or deactivated, as id does not exist in the current view"
    Application.ActiveExplorer.AddToSelection olitem 
exit_sub:
exit_sub:
End Sub

Only after the error the new Folder is opened but does not select certain mail.

Upvotes: 1

Views: 1380

Answers (3)

Traveler
Traveler

Reputation: 245

I had the same issue and found out that Outlook must be given time to bring up the new Display. This can be done using DoEvents. For me, the following works:

Option Explicit
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
Sub ordner_mail_oeffnen()
    
    Dim olitem As Outlook.MailItem
    Set olitem = Outlook.Application.ActiveInspector.CurrentItem

    Dim olfolder As MAPIFolder
    Set olfolder = olitem.Parent
    
    olfolder.Display
    
    'Sleep 10000             ' does not help
    'MsgBox ("Interruption") ' does not help
    DoEvents                 ' Important!

    If Application.ActiveExplorer.IsItemSelectableInView(olitem) = False Then
        Stop ' We should not get here!
             ' But we will, if the line <DoEvents> is missing.
    End If
    
    Application.ActiveExplorer.ClearSelection
    Application.ActiveExplorer.AddToSelection olitem
End Sub

If you omit the DoEvents, the code will run into the Stop command. A previous Sleep or MsgBox will not help. Caveat: when you debug the code step by step (F8), the initial problem will not show up.

Upvotes: 0

niton
niton

Reputation: 9179

You could continue to use GetPath(olitem) and GetFolder(FolderPath) but since the code was not included I cannot be sure.

Replace olfolder.Display with Set ActiveExplorer = olfolder.

Without GetPath(olitem) and GetFolder(FolderPath).

Option Explicit

Sub ordner_mail_oeffnen()

    Dim olitem As Object
    Dim olfolder As Folder

    Set olitem = ActiveInspector.CurrentItem
    Set olfolder = olitem.Parent
    Set ActiveExplorer = olfolder

    ActiveExplorer.ClearSelection
    ActiveExplorer.AddToSelection olitem

End Sub

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Use Explorer.ClearSelection and Explorer.AddToSelection to select an item.

The current Explorer is returned from Application.ActiveExplorer.

Upvotes: 2

Related Questions