VBA Excel-在Outlook中打开特定的文件夹或子文件夹

时间:2018-08-24 17:14:10

标签: excel-vba outlook

我正在尝试从Excel中的VBA宏打开Outlook。我可以打开它,但是如何将其转到特定的文件夹?假设“已发送邮件”,“草稿文件夹”等。另外,如何选择另一个邮箱中的文件夹?我的Outlook中有两个邮箱。

这是我到目前为止的代码:

Sub my_prov_openOutlook()

Dim oOutlook As Object

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOutlook Is Nothing Then
    Shell ("OUTLOOK")
    '''I would like to open a specific folder under the inbox or under a subfolder
Else
    oOutlook.ActiveWindow.Activate
    '''I would like to open a specific folder under the inbox or under a subfolder
End If

1 个答案:

答案 0 :(得分:1)

请勿在Outlook中使用GetObject-使用CreateObject。 Outlook是一个单例:如果它已经在运行,您将获得一个指向现有实例的指针。如果未运行,它将启动。 另外,请勿使用On Error Resume Next-您不会知道是否收到错误以及错误的发生位置。

Set oOutlook = CreateObject("Outlook.Application")
set oNS = oOutlook.GetNamespace("MAPI")
oNS.Logon 'does not do anything if Outlook is already running
set oFolder = oNS.GetDefaultFolder(6) 'olFolderInbox
if (oOutlook.ActiveExplorer Is Nothing) Then
  oFolder.Display
Else
  set oOutlook.ActiveExplorer = oFolder
End If
相关问题