【问题标题】:VBA Excel - Open a specific folder or subfolder in OutlookVBA Excel - 在 Outlook 中打开特定文件夹或子文件夹
【发布时间】:2019-01-31 04:58:21
【问题描述】:

我正在尝试从 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

【问题讨论】:

  • 请使用Try/Catch声明而不是On Error GoTo
  • @Simo 这是 vba,不是 vb.net。

标签: excel vba outlook


【解决方案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

【讨论】:

  • 运行此代码时出现错误。这可能是我的 excel 设置或配置中的某些内容。当代码到达“set oNS = oOutlook.GetNamespace.logon”时,我得到“参数不是可选的”。我使用 GetObject 运行代码,然后将其更改为使用 CreateObject,但仍然出现相同的错误。我使用 CreateObject 设置如下:“set oOutlook = CreateObject ("Outlook.Application")" 对吗?
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 2013-05-06
  • 2022-01-16
  • 2012-06-03
相关资源
最近更新 更多