Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1093
  • Last Modified:

Outlook - Store Custom Fields on Folder to Retrieve Later

I am using Outlook 2003 to handle a manuscript-tracking job.  When I receieve email notification of a new manuscript, I want to programmatically parse the message body and store the manuscript number, title, and author so that I can retrieve that info  later and use it in the body of a template message.  I don't have MS Office installed so I'd rather keep that information inside Outlook if possible.  Can I do something like create some user-defined fields on an Outlook object like a folder?  I am already creating a folder that is the manuscript number, so perhaps that object could also contain user-defined fields for the title and author?

I am attaching two procedures.  The first is FileManuscript, which runs when a new manuscript arrives (thanks to BlueDevilFan).  I added the code to add user properties to the folder.  But I can't really tell if that part has worked.  So I created the second procedure, FindProps, which I intend to run by selecting a folder (like "3345") and then click a custom button that runs the procedure.  But FindProps is not even running - I'm getting runtime errors as soon as I get to the objSelection line.  I have a feeling I am mixing VBA and VB Script (based on different internet examples I've been looking at), and now I don't know HOW to proceed.  

I would sure appreciate some help.  Thanks in advance.
Sub FileManuscript(Item As Outlook.MailItem)
    Dim intPos1 As Integer, _
        intPos2 As Integer, _
        strCaseNumber As String, _
        strEditor As String, _
        objFolder As Object
    Dim customProp1 As Outlook.UserProperty
    Dim customProp2 As Outlook.UserProperty
    Dim customProp3 As Outlook.UserProperty
    intPos1 = InStr(1, Item.Subject, "TRVL-")
    strCaseNumber = Mid(Item.Subject, intPos1 + 5, 4)
    intPos1 = InStr(1, Item.Subject, "editor - ")
    intPos1 = intPos1 + 9
    intPos2 = InStr(1, Item.Subject, ")")
    strEditor = Mid(Item.Subject, intPos1, intPos2 - intPos1)
    Set objFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Parent.Folders(strEditor)
    Set objFolder = objFolder.Folders.Add(strCaseNumber)
    Item.Move objFolder
    
    Set customProp1 = objFolder.UserProperties.Add(custTitle, "test title")
    Set customProp2 = objFolder.UserProperties.Add(custAuthor, "test author")
    Set customProp3 = objFolder.UserProperties.Add(custMS, strCaseNumber)
    
    Set objFolder = Nothing
End Sub
 
Sub FindProps()
    Dim myApp As Outlook.Application
    Dim myNms As Outlook.NameSpace
    Dim myExplorer As Outlook.Explorer
    Dim myFolder As Outlook.MAPIFolder
    Dim objSelection As Selection
    Dim objProperty As Outlook.UserProperty
    
    Set myApp = CreateObject("Outlook.Application")
    Set myNms = myApp.GetNamespace("MAPI")
    Set myExplorer = myApp.ActiveExplorer
 
    Set objSelection = myApp.myExplorer.Selection
    Select Case objSelection.Count
        Case 0 'NOTHING SeLECTED
            strmsg = "No items were selected"
            MsgBox strmsg, , "No selection"
    Case Else
            Set objProperty = objSelection.UserProperties.Find("custTitle")
            If TypeName(objProperty) = "Nothing" Then
                MsgBox "not found"
            Else
                MsgBox "Title is: " & objProperty.Value
            End If
    End Select
    MsgBox "done"
 
End Sub

Open in new window

0
samiam80304
Asked:
samiam80304
  • 5
  • 4
1 Solution
 
David LeeCommented:
Hi, samiam80304.

I'm surprised you aren't getting an error on the code you added to FileManuscript.  Folder objects don't have a UserProperties collection.  That should generate an error.  Only items in a folder (e.g. messages, contacts, etc.) have a UserProperties collection.  You could add properties to the message, but not to the folder you file it in.  That may be a bit confusing because one of the parameters the Add method has is called "AddToFolderFields".  This doesn't add the property to the folder object though, it simply makes it visible to the folder object in order for it to be used in a view.  

Before we spend time fixing code, code that may not be necessary, what do you want to achieve with this?
0
 
samiam80304Author Commented:
Hi BlueDevilFan,
Well, ok - I'm glad I stopped trying to attach user properties to a folder and asked the experts!  What I want to do is store the manuscript title and author somewhere in Outlook as I described in my original question, so I can then access that info later and use it in a template for outgoing messages.  Do you have ideas about how to do that?  One of the messages in the folder will have that information - maybe I just add the user properties to that message?

Thanks again - you are really very helpful.
0
 
David LeeCommented:
"Thanks again - you are really very helpful."
Thanks!

"maybe I just add the user properties to that message"
Yes, adding the properties to that message seems like the best approach.  Here's how to add properties to an item.
Sub FileManuscript(Item As Outlook.MailItem)
    Dim intPos1 As Integer, _
        intPos2 As Integer, _
        strCaseNumber As String, _
        strEditor As String, _
        objFolder As Object, _
        olkProp1 As Outlook.UserProperty, _
        olkProp2 As Outlook.UserProperty, _
        olkProp3 As Outlook.UserProperty
    intPos1 = InStr(1, Item.Subject, "TRVL-")
    strCaseNumber = Mid(Item.Subject, intPos1 + 5, 4)
    intPos1 = InStr(1, Item.Subject, "editor - ")
    intPos1 = intPos1 + 9
    intPos2 = InStr(1, Item.Subject, ")")
    strEditor = Mid(Item.Subject, intPos1, intPos2 - intPos1)
    Set objFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(strEditor)
    Set objFolder = objFolder.Folders.Add(strCaseNumber)
    Set olkProp1 = Item.UserProperties.Add("custTitle", olText)
    olkProp1.Value = "My Title"
    Set olkProp2 = Item.UserProperties.Add("custAuthor", olText)
    olkProp2.Value = "My Author"
    Set olkProp3 = Item.UserProperties.Add("custMS", olText)
    olkProp3.Value = strCaseNumber
    Item.Save
    Item.Move objFolder
    Set objFolder = Nothing
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
samiam80304Author Commented:
Hi,
Sorry for delay.  The message that I am storing that will contain the custom properties actually contains the info I want to store in the message body.  Here is a sample of the body text I receive:
******************************************************************
Manuscript Details :
******************************************************************
TRVL - 2962
Title - Observed impact of Indian Ocean SST variability modes on Asian
monsoon: the role of Indian Ocean Basin Mode and Indian Ocean Dipole Mode Authors - Jianling Yang, Qinyu Liu, Zhengyu Liu Reviewer link: http://www.ametsoc.org/reviewer/viewer.cfm?id=27818-1228

******************************************************************
So given this message text, I am attaching the code you gave me (above) modified so that I could extract the title and authors from the body of the message (I tried to follow the example of how you extracted info from the subject.)  When I run a test, I get this error message from Outlook:
"A program is trying to access email addresses you have stored in OL.  Do you want to allow this?"

Of course I say no.  This error is coming from my incoming test message because it is the only thing that arrives in my Inbox.

Here are my questions:
1.  What is causing this error?  I don't see why the program thinks this procedure is accessing email addresses.  (I have a feeling I  have made some simple mistake.)
2.  The message text may or may not contain Returns/Line Feeds in between the strings I am trying to capture.  I want to ignore those line feeds.  I don't know if that is effecting this test or not - it looks like there might be a line feed in the title.
3.  Once we get past the error and assuming this procedure works, how can I test that it worked (meaning how can I then retrieve the values of those custom properties)?

I continue to send appreciation for your time and effort.

Sub FileManuscript(Item As Outlook.MailItem)
    Dim intPos1 As Integer, _
        intPos2 As Integer, _
        intPos3 As Integer, _
        intPos4 As Integer, _
        intPos5 As Integer, _
        strCaseNumber As String, _
        strEditor As String, _
        strTitle As String, _
        strAuthors As String, _
        objFolder As Object, _
        olkProp1 As Outlook.UserProperty, _
        olkProp2 As Outlook.UserProperty, _
        olkProp3 As Outlook.UserProperty
    intPos1 = InStr(1, Item.Subject, "JCLI-")
    strCaseNumber = Mid(Item.Subject, intPos1 + 5, 4)
    intPos1 = InStr(1, Item.Subject, "editor - ")
    intPos1 = intPos1 + 9
    intPos2 = InStr(1, Item.Subject, ")")
    strEditor = Mid(Item.Subject, intPos1, intPos2 - intPos1)
    
    intPos3 = InStr(1, Item.Body, "Title - ")
    intPos4 = InStr(1, Item.Body, "Authors - ")
    intPos5 = InStr(1, Item.Body, "Reviewer link")
    strTitle = Mid(Item.Body, intPos3 + 8, intPos4 - intPos3)
    strAuthors = Mid(Item.Body, intPos4 + 10, intPos5 - intPos4)
 
    Set objFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(strEditor)
    Set objFolder = objFolder.Folders.Add(strCaseNumber)
   
    Set olkProp1 = Item.UserProperties.Add("custTitle", olText)
    olkProp1.Value = strTitle
    Set olkProp2 = Item.UserProperties.Add("custAuthor", olText)
    olkProp2.Value = strAuthors
    Set olkProp3 = Item.UserProperties.Add("custMS", olText)
    olkProp3.Value = strCaseNumber
    Item.Save
    Item.Move objFolder
    Set objFolder = Nothing
End Sub

Open in new window

0
 
David LeeCommented:
Hi.

"Sorry for delay."
No worries.  

1.  The message isn't an error, it's a warning generated by Outlook's internal security.  Beginning with Outlook 2000's security patch Microsoft had placed certainty security constraints on programs accessing and using Outlook.  This was done to combat the number of viruses and worms written to use Outlook as a means of spreading.  There is no way to turn security off, but there are ways to work around it.  

A.  Sign the code.  Here's a link to instructions on doing that: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_dsvba.asp
B.  Use ClickYes (http://www.contextmagic.com/express-clickyes/), a small utility that'll click the Yes button for you.  It creates a security hole though, since a virus could start sending messages and ClickYes would click the Yes button for it too.  
C.  Use Redemption (http://www.dimastr.com), a COM library that enables code to safely bypass Outlook security.
D.  Disable Outlook's security features.  I don't recommend this one, but it is an option.

2.  Removing the line feed or carriage return/line feed pair is simple.  Something like this will do it:

    strBody = Replace(Item.Body, vbCR, "")
    strBody = Replace(strBody, vbLF, "")

    Insert there two lines at line 22, pushing the rest of the code down.  Then edit what are now lines 22 - 25 replacing all occurrences of Item.Body with strBody.

3.  If you want to see that the values are there, then you can create or edit a view and add these properties as fields so you can see the values in the item list.  If you want to retrieve them via code, then you'll use something like what's in the snippet.  This code prompts you for the name of a property, then displays its value if the property exists in the currently selected item.

   
Sub ViewProp()
    Dim olkItem As Object, olkProp As Outlook.UserProperty, strPropName As String
    strPropName = InputBox("Property name?", "View Property")
    If strPropName <> "" Then
        Set olkItem = Application.ActiveExplorer.Selection(1)
        Set olkProp = olkItem.UserProperties.Find(strPropName)
        If TypeName(olkProp) = "Nothing" Then
            MsgBox "The property " & strPropName & " was not found."
        Else
            MsgBox "The value of the property " & strPropName & " is " & olkProp.Value
        End If
    End If
    Set olkItem = Nothing
    Set olkProp = Nothing
End Sub

Open in new window

0
 
samiam80304Author Commented:
Hi Blue Devil:

1.  The link that you give in A) above to sign the code is a page not found.  I started searching around MSDN and found several articles, (like Intro to Code Signing), but I can't tell if that will apply to Outlook 2003.  Until I figure out that solution, if I just click Yes to the security prompt, should the code run as normally?  It does not appear to be.  The incoming message just sits in the Inbox after I click Yes to the security prompot.

2.  I attached the new ViewProp procedure you gave me to a custom button, and for some reason when I ran my test, Outlook just crashed (meaning it shut itself down and started back up - a feature of Vista I guess).  So I removed the custom button and the next test did not have that result - hard to know if the crash is related to the new Procedure or the button, but I'll focus on that after the first procedure is working again.

I'm attaching the first procedure as it now exists.
Thanks again!
Sub FileManuscript(Item As Outlook.MailItem)
    Dim intPos1 As Integer, _
        intPos2 As Integer, _
        intPos3 As Integer, _
        intPos4 As Integer, _
        intPos5 As Integer, _
        strCaseNumber As String, _
        strEditor As String, _
        strTitle As String, _
        strAuthors As String, _
        objFolder As Object, _
        olkProp1 As Outlook.UserProperty, _
        olkProp2 As Outlook.UserProperty, _
        olkProp3 As Outlook.UserProperty
    intPos1 = InStr(1, Item.Subject, "JCLI-")
    strCaseNumber = Mid(Item.Subject, intPos1 + 5, 4)
    intPos1 = InStr(1, Item.Subject, "editor - ")
    intPos1 = intPos1 + 9
    intPos2 = InStr(1, Item.Subject, ")")
    strEditor = Mid(Item.Subject, intPos1, intPos2 - intPos1)
    
    strBody = Replace(Item.Body, vbCr, "")
    strBody = Replace(strBody, vbLf, "")
    intPos3 = InStr(1, strBody, "Title - ")
    intPos4 = InStr(1, strBody, "Authors - ")
    intPos5 = InStr(1, strBody, "Reviewer link")
    strTitle = Mid(strBody, intPos3 + 8, intPos4 - intPos3)
    strAuthors = Mid(strBody, intPos4 + 10, intPos5 - intPos4)
 
    Set objFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(strEditor)
    Set objFolder = objFolder.Folders.Add(strCaseNumber)
    
    Set olkProp1 = Item.UserProperties.Add("custTitle", olText)
    olkProp1.Value = strTitle
    Set olkProp2 = Item.UserProperties.Add("custAuthor", olText)
    olkProp2.Value = strAuthors
    Set olkProp3 = Item.UserProperties.Add("custMS", olText)
    olkProp3.Value = strCaseNumber
    Item.Save
    Item.Move objFolder
    Set objFolder = Nothing
End Sub

Open in new window

0
 
David LeeCommented:
1.  Microsoft must have moved the content.  I'll look for it and if I can find it I'll post the link.
2.  No reason I can think of for it to crash Outlook.  Hopefully that was an aberration.
0
 
samiam80304Author Commented:
I think I have been given plenty of information to be able to complete the task at hand.  Many thanks for such helpful support.
0
 
David LeeCommented:
Thanks and you're welcome.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now