2

如果这已经发布,请告诉我,因为我无法找到它:)

我在 Outlook 中提示选择当前邮件项目并在打开应用程序之前将其删除:

Dim objApp As Outlook.Application
    Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set CurrentItem = objApp.ActiveExplorer.Selection.item(1)
    Case "Inspector"
        Set CurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Dim mailItem As Outlook.mailItem
Set mailItem = CurrentItem

Dim deleteItem As Boolean
    deleteItem = objApp.mailItem.Delete

If MsgBox("Would you like to move this message to deleted items?", vbYesNo + vbQuestion, "File Indexing") _
        = vbYes Then
            mailItem = deleteItem
            deleteItem = True
        Else
            deleteItem = False
End If

所有这些都完美无缺,但如果没有选择当前项目,我希望出现一个模式窗口,但我不确定如何添加它。它会在同一个 IfThen 中,还是在完全不同的语句中?我试过添加一些类似的东西

If CurrentItem = Null Then
MsgBox ("Please select a mail item")
End If

但随后 MsgBox 永远不会出现并且代码正常执行。感谢您的任何帮助!

编辑:感谢您的回复。不幸的是,我发现了一些与此无关的错误,因此我需要在向 Outlook 按钮添加其他代码之前解决它们。

4

3 回答 3

1

尝试这个

Option Explicit

Private Sub test()

Dim currentItem As Object

Dim objApp As Outlook.Application

Set objApp = Application

On Error Resume Next

Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set currentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set currentItem = objApp.ActiveInspector.currentItem
End Select

On Error GoTo 0

If currentItem Is Nothing Then

    MsgBox ("Please select a mail item")

End If

End Sub
于 2015-09-04T16:32:01.410 回答
1

感谢您的帮助,但我的老板能够想出这个似乎工作正常的小花絮:

Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        If objApp.ActiveExplorer.Selection.Count > 0 Then
            Set currentItem = objApp.ActiveExplorer.Selection.Item(1)
        Else
            MsgBox ("No Messages Selected.")
            Exit Sub
        End If
    Case "Inspector"
        Set currentItem = objApp.ActiveInspector.currentItem
    Case Else
        MsgBox ("Please select a mail item.")
        Exit Sub
于 2015-09-22T14:51:06.600 回答
0

请参阅VBA 检查变量是否为空

你在哪里运行上面列出的代码?它是 NewInspector 事件处理程序吗?

objApp.ActiveExplorer.Selection.item(1)

无论如何,我建议打破属性和方法调用链并将它们声明在单独的代码行中。因此,您会发现哪些属性或方法失败或返回 null。

尝试使用以下代码:

  If Not CurrentItem Is Nothing Then
    ' obj initialized. '
  Else
    MsgBox ("Please select a mail item")
  End If
于 2015-09-05T16:31:54.433 回答