Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

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
Avatar of Bill Prew
Bill Prew

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
Avatar of Mark

ASKER

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?
Avatar of Mark

ASKER

This looks like it gets the current item:

ActiveExplorer.Selection.item(1)

now, to get the next item ...
Avatar of Mark

ASKER

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

~bp
Avatar of Mark

ASKER

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

~bp
Avatar of Mark

ASKER

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 ...
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

Sorry about the delay -- will check this out tomorrow
Avatar of Mark

ASKER

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

~bp