vba Outlook how to select a new current message

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
LVL 1
jmarkfoleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
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
0
jmarkfoleyAuthor 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?
0
jmarkfoleyAuthor Commented:
This looks like it gets the current item:

ActiveExplorer.Selection.item(1)

now, to get the next item ...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jmarkfoleyAuthor 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?
0
Bill PrewCommented:
Well, you could probably figure out which Item the current highlighted is, but how does that help?

~bp
0
jmarkfoleyAuthor 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.
0
Bill PrewCommented:
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
0
Bill PrewCommented:
Actually, I may have an idea for this, let me try something here and see if it works as I think it might...

~bp
0
jmarkfoleyAuthor 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 ...
0
Bill PrewCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmarkfoleyAuthor Commented:
Sorry about the delay -- will check this out tomorrow
0
jmarkfoleyAuthor 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
0
Bill PrewCommented:
Welcome.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.