Solved

Outlook VBA sort v4.0

Posted on 2010-08-16
16
755 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Fveng
  • 9
  • 7
16 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 33449041
Hello again.

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

Author Comment

by:Fveng
ID: 33449835
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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33449905
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

0
 

Author Comment

by:Fveng
ID: 33454656
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?
0
 

Author Comment

by:Fveng
ID: 33455009
also, code fails to execute when an attachment is present
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33459883
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?
0
 

Author Comment

by:Fveng
ID: 33463967
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
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33464071
That sounds like a good thing!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

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

Author Comment

by:Fveng
ID: 33464850
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?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33465069
"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.
0
 

Author Comment

by:Fveng
ID: 33465152
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?
0
 

Author Comment

by:Fveng
ID: 33465165
as in, firing when the 'the selected item is a mail item' currently fires?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33465367
"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?
0
 

Author Comment

by:Fveng
ID: 33465468
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!
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 33608651
Sorry, I lost track of this question.  Replace all the code you have now, except for ClassifyAndFile_V2, with the code below.  This code will pop up the dialog-box each time an unread item is selected.  The code will not work if you open a new Outlook window.
Dim WithEvents olkExplorer As Outlook.Explorer

Private Sub olkExplorer_SelectionChange()
    On Error Resume Next
    If Application.ActiveExplorer.Selection(1).Class = olMail Then
        If Application.ActiveExplorer.Selection(1).UnRead Then
            ClassifyAndFile_v2 Application.ActiveExplorer.Selection(1)
        End If
    End If
    On Error GoTo 0
End Sub

Private Sub Application_Quit()
    Set olkExplorer = Nothing
End Sub

Private Sub Application_Startup()
    Set olkExplorer = Application.ActiveExplorer
End Sub

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now