【问题标题】:Outlook 2007+ Cast Application.ActiveExplorer.Selection.Item to Outlook.mailItem when TypeOf Item = Outlook.mailItem当 TypeOf Item = Outlook.mailItem 时,Outlook 2007+ 将 Application.ActiveExplorer.Selection.Item 转换为 Outlook.mailItem
【发布时间】:2020-06-29 15:50:02
【问题描述】:

采取以下outlook vba:

Sub FileEmails()
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection

    Set myOlExp = Application.ActiveExplorer
    Set myOlSel = myOlExp.Selection

    If myOlSel.Count = 0 Then
        MsgBox "No objects selected."
    Else
        For Each SelectedItem In myOlSel
            If (TypeOf SelectedItem Is Outlook.mailItem) Then
                Dim mailItem As Outlook.mailItem
                Set mailItem = SelectedItem
                itemMessage = "The item is an e-mail message. The subject is " & mailItem.Subject & "."
                mailItem.Display (False)
            ElseIf (TypeOf SelectedItem Is Outlook.contactItem) Then
                Dim contactItem As Outlook.contactItem
                Set contactItem = SelectedItem
                itemMessage = "The item is a contact. The full name is " & contactItem.Subject & "."
                contactItem.Display (False)
            ElseIf (TypeOf SelectedItem Is Outlook.AppointmentItem) Then
                Dim apptItem As Outlook.AppointmentItem
                Set apptItem = SelectedItem
                itemMessage = "The item is an appointment." & apptItem.Subject & "."
            ElseIf (TypeOf SelectedItem Is Outlook.taskItem) Then
                Dim taskItem As Outlook.taskItem
                Set taskItem = SelectedItem
                itemMessage = "The item is a task. The body is " & taskItem.Body & "."
            ElseIf (TypeOf SelectedItem Is Outlook.meetingItem) Then
                Dim meetingItem As Outlook.meetingItem
                Set meetingItem = SelectedItem
                itemMessage = "The item is a meeting item. The subject is " & meetingItem.Subject & "."
            End If
        Next SelectedItem

        expMessage = expMessage & itemMessage
        MsgBox (expMessage)
    End If

End Sub

如果我在收件箱中选择了一些项目并运行此代码,它会成功识别 SelectedItem 是 Outlook.mailItem,但在尝试将 SelectedItem 转换为 Outlook.MailItem 时出现以下错误(即使返回了 typeof 参数真的):

Object variable or with block variable not set

我怎样才能执行这个演员?我将此代码基于以下 .net 示例(使用 TryCast):

http://msdn.microsoft.com/en-us/library/ms268994.aspx

【问题讨论】:

  • 我尚未测试您的代码,但这里有几点可能会有所帮助。 Outlook VBA 与 VB.NET 不同。 VB.NET 是较新的一代,并有许多改进。其中一项改进是您可以在块级别声明变量。对于 VBA,变量只能在模块或例程级别声明。我不知道如果你在循环中重新声明变量会发生什么,所以将所有 Dim 语句移到顶部。 VB.NET 不使用SET。 VBA 需要 SET 用于对象,因此请尝试使用 Set mailItem = SelectedItem
  • 嗨,托尼,内联 Dims 很好(尽管无论如何我会将它们移到顶部),是缺少 SET 导致了问题(Doh!) - 我不介意但我多年来一直在编写 .net 和经典的 asp 应用程序 - 很惊讶我自己没有注意到这一点(而且有点尴尬!) - 无论如何 - 将其发布为答案 & 我会将其标记为解决方案。干杯。

标签: vba outlook


【解决方案1】:

我尚未测试您的代码,但这里有几点可能会有所帮助。

Outlook VBA 与 VB.NET 不同。 VB.NET 是较新的一代,并有许多改进。

其中一项改进是您可以在块级别声明变量。对于 VBA,变量只能在模块或例程级别声明。我不知道如果你在循环中重新声明变量会发生什么,所以将所有 Dim 语句移到顶部。

VB.NET 不使用 SET。 VBA 需要 SET 对象,因此请尝试 Set mailItem = SelectedItem。

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2019-12-21
    • 1970-01-01
    • 2014-10-13
    • 2019-04-03
    • 1970-01-01
    • 2011-01-12
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多