Link to home
Start Free TrialLog in
Avatar of Fveng
FvengFlag for United States of America

asked on

Outlook VBA sort v4.0

Code now being used for only Outlook 2007,  the current code has managed to open the dialog box which states what type, etc. the message is, but has yet to automatically open a box requiring the job number to be input. I am now running the code on Outlook 2007 on a Windows 7 PC.

What I would like is for a dialog box to automatically pop up when an 'unread' message is opened.

Code is attached.
Private WithEvents olkMailItem As Outlook.MailItem

' Still in thisOutlookSession but now at the application level '
Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail Then Set olkMailItem = Item
End Sub


'Private Sub Application_ItemLoad(ByVal Item As Object)
    'MsgBox "The selected item is of Class: " & Item.Class & " and MessageClass: " & Item.MessageClass
'End Sub

Private Sub olkMailItem_PropertyChange(ByVal Name As String)
    If Name = "UnRead" Then
        If olkMailItem.UnRead Then
            ClassifyAndFile_v2 olkMailItem
            
        End If
    End If
End Sub




Sub ClassifyAndFile_v2(olkItm As Object)

    'On the next line edit the root folder path.  This is the older that all project folders are under.'

  Const ROOT_FOLDER = "H:\Email test"
  
    Dim strFileCode As String, strFilePath As String, intCnt As Integer
    If olkItm.Class = olMail Then
        strFileCode = ""
        strFileCode = InputBox("Enter the filing code for the item" & vbCrLf & olkItm.Subject, "Classify and File")
        If strFileCode <> "" Then
            strFilePath = ROOT_FOLDER & strFileCode & "\" & olkItm.Subject & ".msg"


            'Save the original message'
            olkItm.SaveAs strFilePath, olMSG


            'Insert a link to the original message at the bottom of the message'
            olkItm.HTMLBody = olkItm.HTMLBody & "<a href=""file://" & strFilePath & """>Original Message</a><br>"
            olkItm.Save


            'Remove the attachments'
            For intCnt = olkItm.Attachments.Count To 1 Step -1
                olkItm.Attachments.Item(intCnt).Delete
            Next
            
        End If
    End If

    Set olkItm = Nothing
    Set olkFld = Nothing

End Sub


'Private Sub Application_Startup()
    'MsgBox "Macros are enabled."
'End Sub

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Hello again.

How are you opening the items?  Are you selecting them and then opening, or selecting and opening all at once?
Avatar of Fveng

ASKER

just selecting one then it opens in the right hand inset window, you know? I also checked to see that the message identification script worked, and it did, but it ran for every email, not just the unread ones.
Ok.  Swap the code below for the code you have now.  Select an unread message and tell me what happens.
Private WithEvents olkMailItem As Outlook.MailItem

' Still in thisOutlookSession but now at the application level '
Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail Then 
        Set olkMailItem = Item
        Msgbox "The selected item is a mail item."
    Else
        Msgbox "The selected item is not a mail item."
    End If
End Sub

Private Sub olkMailItem_PropertyChange(ByVal Name As String)
    Msgbox "The changed property is: " & Name
    If Name = "UnRead" Then
        Msgbox "The value of UnRead is " & OlkMailItem.UnRead
        If olkMailItem.UnRead Then
            ClassifyAndFile_v2 olkMailItem
        End If
    End If
End Sub

Sub ClassifyAndFile_v2(olkItm As Object)

    'On the next line edit the root folder path.  This is the older that all project folders are under.'

  Const ROOT_FOLDER = "H:\Email test"
  
    Dim strFileCode As String, strFilePath As String, intCnt As Integer
    If olkItm.Class = olMail Then
        strFileCode = ""
        strFileCode = InputBox("Enter the filing code for the item" & vbCrLf & olkItm.Subject, "Classify and File")
        If strFileCode <> "" Then
            strFilePath = ROOT_FOLDER & strFileCode & "\" & olkItm.Subject & ".msg"


            'Save the original message'
            olkItm.SaveAs strFilePath, olMSG


            'Insert a link to the original message at the bottom of the message'
            olkItm.HTMLBody = olkItm.HTMLBody & "<a href=""file://" & strFilePath & """>Original Message</a><br>"
            olkItm.Save


            'Remove the attachments'
            For intCnt = olkItm.Attachments.Count To 1 Step -1
                olkItm.Attachments.Item(intCnt).Delete
            Next
            
        End If
    End If

    Set olkItm = Nothing
    Set olkFld = Nothing

End Sub


'Private Sub Application_Startup()
    'MsgBox "Macros are enabled."
'End Sub

Open in new window

Avatar of Fveng

ASKER

I managed to get this script to run, but it will only execute if I mark the message that is currently selected as unread. I wonder if initially the tag on the message is not 'Unread' but rather, something else?
Avatar of Fveng

ASKER

also, code fails to execute when an attachment is present
Something is horribly wrong at your end.  It's impossible for the presence of an attachment to interfere with this code.  The UnRead property is a boolean value.  It can only be True (unread) or False (read).  

"it will only execute if I mark the message that is currently selected as unread"
Yes, the message must be unread for this to work.  Unless I've completely misunderstood, that's what you wanted.  When a message arrives it is unread.  My understanding is that you want the code to fire as sson as the user opens (reads) the message.  

The above aside, when the code did fire what message boxes popped up and what did they say?
Avatar of Fveng

ASKER

ok, so I reloaded the code this morning and now every time I click an email it says 'the selected item is a mail item'  which it did not do yesterday, so being an optimist, I'll assume this is a good thing. :D
That sounds like a good thing!
Avatar of Fveng

ASKER

ok, so it still only recognizes the value of UnRead as 'True' if I mark the message 'UnRead' after it has been read.thoughts?
Avatar of Fveng

ASKER

also, I had a thought on how to identify an unread message without actually going into the details of the message. Is there a way that we could just have the program see if the subject of the message is bold?
"ok, so it still only recognizes the value of UnRead as 'True' if I mark the message 'UnRead' after it has been read.thoughts?"

I guess I don't understand.  As I said in my post last night unless I've completely misunderstood what there records are supposed to be unread already.  If the message has already been read, then the code isn't going to fire because it is keying on the transition between unread and read.  Here's teh sequence of events.

1.  The user selects a record.  That fires the code in the ItemLoad subroutine.  This sets a watch on the currently selected item.
2.  The user opens the selected message.  This fires the code in the PropertyChange subroutine since the UnRead property is being changed from True to False.  


"Is there a way that we could just have the program see if the subject of the message is bold?"

No, that's not possible.
Avatar of Fveng

ASKER

OK, so I see what you're meaning with the 'Opens the selected message', what I'm looking for is it to fire when the user 'Selects the record', is that possible?
Avatar of Fveng

ASKER

as in, firing when the 'the selected item is a mail item' currently fires?
"Selects the record"

I assume that means selects a message.  If so, then that would mean that the code would fire even before the item opens so the user can see what it is.  Are you sure that's what you want?
Avatar of Fveng

ASKER

Yes, because the dialog box is small enough that the user can still drag it around to see what the message is about, and can also be closed if the message does not need to be sorted.

Sorry for any confusion!
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

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