We help IT Professionals succeed at work.

vba Outlook how to select a new current message

Mark
Mark asked
on
I have the following VBA code:
Public Sub scanFolder(thisCategory as String)
Dim src As Folder
Dim oItem As Object
Dim propertyAccessor As Outlook.propertyAccessor
Set src = Application.ActiveExplorer.CurrentFolder
Dim strHeader As String

For Each oItem In src.Items
    If TypeOf oItem Is Outlook.MailItem Then
        Set propertyAccessor = oItem.propertyAccessor
        header = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
        Dim headerLines() As String
        headerLines() = Split(header, vbCrLf)

        Dim thisHeader As Variant

        For Each thisHeader In headerLines
            If inStr(thisHeader, thisCategory) > 0 then
                  ' set this message as the currently highlighted on in mail folder
            End If
        Next
    End If
Next
End Sub

Open in new window

I want to do 2 things:

1. instead of scanning from the beginning of the selected folder (For Each oItem In src.Items) I want to begin scanning with the message following the currently selected one.

2.  Where I have "set this message ...", I want to set the currently highlighted/selected message in the mail folder to the current oItem.

Thanks
Comment
Watch Question

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I don't think that's possible.  I believe the collection of objects in the Folder will always be presented in the same order, and not sure what that is, could be random.  However in the Outlook UI you can sort the displayed messages in any number of sequences.  That will not change the order the messages are seen in the VBA script though.  So I don't think there is any way to skip the ones "above" the current ones in the UI, during the VBA script.

I'll do a little research but I suspect that is the case.  You could potentially skip over ones that are marked as Read, but not sure if that helps?

~bp

Author

Commented:
Well,that would kind of suck. What I ultimately want to do is search for some user specified criterion, select that as the current message, then continue from there if the user want to find-next. I suppose I could keep track of the last folder and last item, but can a vba macro save persistent values during an outlook session?

Author

Commented:
This looks like it gets the current item:

ActiveExplorer.Selection.item(1)

now, to get the next item ...

Author

Commented:
OK, you're right. Apparently no real way to relate current item to its position in the CurrentFolder that I can see. Once again, hats off to Microsoft for their ability to add complexity ...

How about this ...

1. given For Each item in Application.ActiveExplorer.CurrentFolder.Items, is there a way to determine if the selected 'item' in the loop is also the selected/highlighted message in the folder? Some attribute perhaps?

2. given the loop variable 'item', above, is there a way to set this as the currently selected/highlighted message in the folder?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Well, you could probably figure out which Item the current highlighted is, but how does that help?

~bp

Author

Commented:
bill Prew:
Well, you could probably figure out which Item the current highlighted is, but how does that help?
Because, assuming the current item is <curitem> (below), I could then ...

For Each oItem In Application.ActiveExplorer.CurrentFolder.Items
    if oItem = <curItem> then continue
' if I get here I'm at the item after the current highlighted item
Next

That would be half the problem solved.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Yes, but you have no idea what order the collection will present the email items in, I don't believe.  I'm not even sure it's repeatable.

~bp
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Actually, I may have an idea for this, let me try something here and see if it works as I think it might...

~bp

Author

Commented:
Bill Prew:
you have no idea what order the collection will present the email items in
Whether or not it's the order shown on the view windows, my experiments indicate that the VB code seems to scan it in a consistent order.

Awaiting your idea ...
Test your restores, not your backups...
Top Expert 2016
Commented:
Not sure this is going to get the job done, but wanted to share what I had since running low on time today to poke further.  Perhaps you can experiment with it a bit...

Public Sub scanFolder(thisCategory As String)
   Dim src As Folder
   Dim oItem As Object
   Dim propertyAccessor As Outlook.propertyAccessor
   Set src = Application.ActiveExplorer.CurrentFolder
   Dim strHeader As String

   src.Items.Sort "[ReceivedTime]", True

   Dim bFoundCurrent As Integer
   Dim oCurrentItem As Object
   bFoundCurrent = 0
   Set oCurrentItem = ActiveExplorer.Selection.Item(1)


   For Each oItem In src.Items
      If TypeOf oItem Is Outlook.MailItem Then
         If bFoundCurrent = 0 Then
            If oItem.EntryID = oCurrentItem.EntryID Then
               bFoundCurrent = 1
            End If
         Else
            Set propertyAccessor = oItem.propertyAccessor
            header = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
            Dim headerLines() As String
            headerLines() = Split(header, vbCrLf)

            Dim thisHeader As Variant

            For Each thisHeader In headerLines
               If InStr(thisHeader, thisCategory) > 0 Then
                  ' set this message as the currently highlighted on in mail folder
                  Application.ActiveExplorer.ClearSelection
                  Application.ActiveExplorer.AddToSelection oItem
                  Application.ActiveExplorer.RemoveFromSelection oCurrentItem
                  Exit Sub
               End If
            Next
         End If
      End If
   Next
End Sub                                  

Open in new window

~bp

Author

Commented:
Sorry about the delay -- will check this out tomorrow

Author

Commented:
I'm likewise going to play with this some more later. The project labor level is growing and I did find another way of accomplishing my goal using 'Filter E-mail'. Thanks you've been a great help on this VBA stuff
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome.

~bp