Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何打开当前打开项目的文件夹并选择该项目?_Vba_Outlook - Fatal编程技术网

Vba 如何打开当前打开项目的文件夹并选择该项目?

Vba 如何打开当前打开项目的文件夹并选择该项目?,vba,outlook,Vba,Outlook,我有一个子文件夹,用于打开当前打开的邮件项目的文件夹 如果我打开了一个项目,但其间更改了邮件文件夹,并且希望立即再次打开正确的文件夹,这是有意义的 Sub-ordner\u-mail\u-oeffnen() 错误转到退出\ U sub '将olApp设置为Outlook.Application 将我设置为Outlook.mailitem '设置olApp=Outlook.Application 设置m=Outlook.Application.ActiveInspector.CurrentItem

我有一个子文件夹,用于打开当前打开的邮件项目的文件夹

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

Sub-ordner\u-mail\u-oeffnen()
错误转到退出\ U sub
'将olApp设置为Outlook.Application
将我设置为Outlook.mailitem
'设置olApp=Outlook.Application
设置m=Outlook.Application.ActiveInspector.CurrentItem
将文件夹设置为MAPIFolder
将FolderPath设置为字符串
将子文件夹设置为Outlook.MAPIFolder
FolderPath=GetPath(m)
设置olfolder=GetFolder(FolderPath)
olfolder.Display
“这两条线只是为了测试
MsgBox“jetzt”
Application.ActiveExplorer.ClearSelection
睡眠(10000)
Application.ActiveExplorer.ClearSelection
'出现运行时错误(我试图翻译)“-2147467259(80004005)元素无法激活或停用,因为当前视图中不存在id”
Application.ActiveExplorer.AddToSM
出口接头:
出口接头:
端接头

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

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


当前资源管理器是从
应用程序返回的。ActiveExplorer

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

替换
olfolder。显示
Set-ActiveExplorer=olfolder

没有
GetPath(olitem)
GetFolder(FolderPath)

选项显式
子订单(邮件)
作为对象
将文件夹设置为文件夹
设置olitem=ActiveInspector.CurrentItem
设置olfolder=olitem.Parent
设置ActiveExplorer=olfolder
ActiveExplorer.exe
ActiveExplorer.AddToSM
端接头

我也有同样的问题,我发现必须给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
选项显式
公共声明PtrSafe子睡眠库“kernel32”(ByVal毫秒为LongPtr)
子订单(邮件)
将我设置为Outlook.MailItem
设置m=Outlook.Application.ActiveInspector.CurrentItem
将文件夹设置为MAPIFolder
设置olfolder=olitem.Parent
olfolder.Display
“睡眠10000”没有帮助
“MsgBox(“中断”)没有帮助
这很重要!
如果Application.ActiveExplorer.IsItemSelectableInView(olitem)=False,则
停下来,我们不应该到这里!
“但如果这条线不见了,我们会的。
如果结束
Application.ActiveExplorer.ClearSelection
Application.ActiveExplorer.AddToSM
端接头
如果省略
DoEvents
,代码将运行到
Stop
命令中。以前的
Sleep
MsgBox
没有帮助。
警告:当您一步一步地调试代码(F8)时,最初的问题不会出现。

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