Search code examples
vbaoutlookcurrentitem

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:

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? 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. 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.


Solution

  • 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