首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打开当前打开项目的文件夹并选择该项目?

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

Stack Overflow用户
提问于 2014-06-12 22:13:39
回答 3查看 862关注 0票数 1

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

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

代码语言:javascript
复制
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

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

EN

回答 3

Stack Overflow用户

发布于 2014-06-12 22:23:32

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

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

票数 2
EN

Stack Overflow用户

发布于 2019-08-11 09:04:12

您可以继续使用GetPath(olitem)GetFolder(FolderPath),但是因为没有包含代码,所以我不能确定。

Set ActiveExplorer = olfolder替换olfolder.Display

没有GetPath(olitem)GetFolder(FolderPath)

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2020-12-23 01:30:10

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

代码语言:javascript
复制
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)时,不会出现初始问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24186649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档