Outlook vba打开文件夹但不选择某些邮件

时间:2014-06-12 14:13:39

标签: outlook-vba

我有一个Sub打开当前打开的邮件项目的文件夹;如果我打开了一个项目,但是在中间更改了mail-Folder,并希望再次立即打开正确的文件夹,这是有道理的。

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

仅在错误之后打开新文件夹。 有任何想法吗? 谢谢 最大

3 个答案:

答案 0 :(得分:1)

使用Explorer.ClearSelection和Explorer.AddToSelection选择项目。

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

答案 1 :(得分:0)

您可以继续使用x,lt,ut = map(abs, [df_log[i,j], lower_threshold, upper_threshold]) if any(0.95*t < x < 1.05*t for t in [lt, ut]): print("Error detected") GetPath(olitem),但是由于未包含代码,所以我不确定。

GetFolder(FolderPath)替换为olfolder.Display

没有Set ActiveExplorer = olfolderGetPath(olitem)

GetFolder(FolderPath)

答案 2 :(得分:0)

我遇到了同样的问题,发现必须给 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),最初的问题不会出现。