简体   繁体   中英

Open “Add reminder…” dialog for mail item in Outlook using VBA?

Using Outlook 2013 32-bit under Win10 64-bit.

In VBA, suppose I have a mailitem object. I know I can set the various task/reminder options individually through my own form if I want to create one. It seems like it would be a lot easier if I could cause Outlook's "Add reminder ..." dialog to display for the mail item, but I haven't found any references to it.

This is the dialog as it appears in the Outlook: 添加提醒对话框

(As an aside, I can get the Categories dialog to come up using myMailItem.ShowCategoriesDialog, but there doesn't seem to be anything similar for reminders.)

Thanks!

When you click the Add Reminder dialog box button on the ribbon, depending on what window is active it will act and will tie the reminder object (and/or follow up object etc) to a mail item.

If you have no mail item open it will tie it to the mail items that are selected in the inbox (one or more), By default only one item is selected, which is highlighted with a gray or light blue background. You can hold the CTRL key down and select more mail items and then click the Add Reminder to apply the reminder to all of those items.

If a mail item is already open and active then the reminder will be applied only to that mail item. For your case that it seems you want to apply the reminders one by one we will go with the latter method. Find the mail item we want and then open it, and then open the Add Reminder dialog box, and when you hit ok, it will save it and close the active window. This code finds the most recent mail item, but you should be able to change the selection criteria:

Private Sub PopUpReminderDialogBox()
    Dim iLatest As Long
    Dim myFolder As Object
    Dim myNameSpace As Object
    Dim myItem As Outlook.MailItem
    Dim olApp As Outlook.Application

    Set olApp = Outlook.Application 'you can initiate a New application too
    Set myNameSpace = olApp.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

    iLatest = myFolder.Items.Count
    Set myItem = myFolder.Items(iLatest)

    myItem.Display 'to make the mailitem currentitem
    olApp.ActiveInspector.CommandBars.ExecuteMso ("AddReminder")
    myItem.Close olSave
End Sub

If you wanted by any chance to open up the reminder dialog box when one or more mail item is highlighted in the inbox, then you need to use

    olApp.ActiveExplorer.CommandBars.ExecuteMso ("AddReminder")

instead of

    olApp.ActiveInspector.CommandBars.ExecuteMso ("AddReminder")

Thank you, @ibo. That was very helpful, so I have accepted it as the answer.

Here is a mock-up of a macro that simulates what I wanted to do. The fact that it changes the explorer that is visible in the Outlook UI whenever the code points to a different folder was useful in my case, but in the general case it would be better to have a method that didn't use the UI at all. I just don't think there is one.

<========================== EDITED =========================>

I found out by experimentation that changing the folder in the active explorer actually takes a lot of time (from the CPU perspective), so in many cases the explorer is not ready to clear the selection and select the chosen item unless you force the macro to wait. Usually a second or two is enough. I modified the code to allow up to 10 seconds.

I also learned that the EntryID, as the unique identifier of a folder, is a good alternative to using the Is operator, which does not work whenever two objects were not "Set" equal to each other.

Public Sub testMSO()

'This test macro selects the last item in the Inbox, but it could be
' any item in any available folder.

Dim loItem As Object
Dim loNewFold As Folder
Dim loOldFold As Folder
Dim loExplorer As Explorer
Dim lbFoldChg As Boolean
Dim liLoops As Integer
Dim ldWait As Date

On Error Resume Next 'Mimimal error handling, just for demonstration purposes

'Get the Inbox folder.
Set loNewFold = Session.GetDefaultFolder(olFolderInbox)

'Get the active explorer.
Set loExplorer = Application.ActiveExplorer

'The current and "new" folders always compare as different using the Is operator
' because Is determines whether two objects were Set equal, so just use the
' unique EntryID to determine if they are really different.
If loExplorer.CurrentFolder.EntryID = loNewFold.EntryID Then
    lbFoldChg = False
Else
    lbFoldChg = True

    'Save the explorer's current folder
    Set loOldFold = loExplorer.CurrentFolder

    'Assign the Inbox to the active explorer. Note that this is preferable to the
    ' loNewFold.Display method, which is disconcerting to the user because it opens
    ' a new explorer (new Outlook window).
    Set loExplorer.CurrentFolder = loNewFold

End If

'Arbitrarily pick the last Inbox item.
Set loItem = loNewFold.Items(loNewFold.Items.Count)

'Make the chosen item the only selected item.
If lbFoldChg Then

    'Selection takes place in the Outlook UI, so if the folder changes, it takes
    ' quite a while in CPU terms to load the explorer and get it ready to change
    ' the selection, so wait up to 10 seconds in 1-second increments.

    For liLoops = 1 To 10
        If loExplorer.IsItemSelectableInView(loItem) Then
            loExplorer.ClearSelection
            loExplorer.AddToSelection loItem
            Exit For
        Else
            ldWait = Now + (1 / 86400)  'Add a second to current date/time
            Do While Now < ldWait
                DoEvents
            Loop
        End If
    Next liLoops

    If liLoops = 11 Then
        MsgBox "Unable to select mail item in folder " & loItem.Parent.Name, _
                vbOKOnly + vbExclamation
        GoTo cleanup
    End If

Else

    'If the folder has not changed, no need to wait.
    loExplorer.ClearSelection
    loExplorer.AddToSelection loItem

End If

'Display the Add Reminder dialog if it is available. It might not be available
' if the item is, for example, an meeting item, or if the chosen folder does
' not support reminders.
If loExplorer.CommandBars.GetEnabledMso("AddReminder") Then
    loExplorer.CommandBars.ExecuteMso "AddReminder"
End If

cleanup:

If lbFoldChg Then
    'Point the explorer back to the original folder.
    Set loExplorer.CurrentFolder = loOldFold
End If

'Clean up
Set loExplorer = Nothing
Set loNewFold = Nothing
Set loOldFold = Nothing
Set loItem = Nothing

End Sub 'testMSO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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