简体   繁体   English

VBA如何:在Outlook中的ActiveInspector.CurrentItem为null时提示?

[英]VBA How To: Prompt When ActiveInspector.CurrentItem is null in Outlook?

If this has been posted, please let me know as I wasn't able to find it :) 如果已发布,请告诉我,因为我找不到它:)

I've made a prompt in Outlook that selects the current mail item and deletes it before opening an application: 我已经在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

All of that works perfectly, but I would like a modal window to appear if there is no current item selected , but I'm not sure how to add that in. Would it be within the same IfThen, or a completely different statement? 所有这些都可以正常运行,但是如果没有选择当前项 ,我希望出现一个模式窗口,但是我不确定如何添加它。它是否在同一IfThen或完全不同的语句中? I've tried adding something along the lines of 我试图添加一些类似的东西

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

but then the MsgBox never appears and the code executes normally. 但是MsgBox永远不会出现,并且代码可以正常执行。 Thank you for any help! 感谢您的任何帮助!

EDIT: Thanks for the responses. 编辑:感谢您的答复。 Unfortunately I found a couple of errors unrelated to this so I need to address them before I add additional code to my Outlook button. 不幸的是,我发现了几个与此无关的错误,因此在将其他代码添加到Outlook按钮之前,需要解决它们。

Try this 尝试这个

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

See VBA Check if variable is empty . 请参见VBA。检查变量是否为空

Where do you run the code listed above? 您在哪里运行上面列出的代码? Is it the NewInspector event handler? 是NewInspector事件处理程序吗?

objApp.ActiveExplorer.Selection.item(1) objApp.ActiveExplorer.Selection.item(1)

Anyway, I'd suggest breaking the chain of property and method calls and declaring them on separate line of code. 无论如何,我建议打破属性和方法调用的链条,并在单独的代码行中声明它们。 So, you will find what property or method fails or returns null. 因此,您将发现什么属性或方法失败或返回null。

Try to use the following code: 尝试使用以下代码:

  If Not CurrentItem Is Nothing Then
    ' obj initialized. '
  Else
    MsgBox ("Please select a mail item")
  End If

Thanks for the help, but my boss was able to come up with this little tid-bit that seems to be working properly: 感谢您的帮助,但我的老板提出了一些似乎可以正常工作的提示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM