【问题标题】:How to open the folder of current open item and select that item?如何打开当前打开项目的文件夹并选择该项目?
【发布时间】:2026-02-13 22:00:01
【问题描述】:

我有一个 Sub 可以打开当前打开的邮件项的文件夹。

如果我打开了一个项目,但更改了中间的邮件文件夹,并且想立即再次打开正确的文件夹,这是有意义的。

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

只有在出现错误后,新文件夹才会打开,但不会选择某些邮件。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    使用Explorer.ClearSelectionExplorer.AddToSelection 选择一个项目。

    当前资源管理器从Application.ActiveExplorer返回。

    【讨论】:

    • 非常感谢,正朝着正确的方向前进。但是:“显示”似乎工作得很晚,即使我在此之后放置了一个 msgbox 和一个 sleep(10000),新的资源管理器也只会在代码末尾显示。因此,在尝试 .AddToSelection 时出现错误。我编辑上面的问题...
    • 您可以通过将 Application.ActiveExplorer.CurrentFolder 属性设置为 olfolder 来重用当前文件夹,而不是在新窗口中显示文件夹。
    • 除了使用 MAPIFolder.Display,您还可以使用 MAPIFolder.GetExplorer 后跟 Explorer.Display 并使用它来代替 Application.ActiveExplorer
    【解决方案2】:

    您可以继续使用GetPath(olitem)GetFolder(FolderPath),但由于未包含代码,我无法确定。

    olfolder.Display 替换为Set ActiveExplorer = olfolder

    没有GetPath(olitem)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
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,发现必须给 Outlook 时间来启动新的显示。这可以使用DoEvents 来完成。对我来说,以下工作:

      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
      

      如果省略DoEvents,代码将运行到Stop 命令。以前的 SleepMsgBox 将无济于事。 警告:当您逐步调试代码时(F8),最初的问题不会出现。

      【讨论】:

        最近更新 更多