UserProperties VBA to move message to another folder

Status
Not open for further replies.

mrmmickle1

Member
Outlook version
Outlook 2010 64 bit
Email Account
Office 365 Exchange
I have created a custom form that I use to document Reporting Categories.

These Categories are then concatenated and put into a UserProperty.

I would like to then use the Application_ItemSend Event in order to perform something similar to the following rule:

Apply the rule after I send the message with ‡ in the body and on this computer only move a copy to the Deliverable Folder

Note: I was initially concatenating the fields in my form into the body of the email like this....which actually worked... I would rather save these to a UserProperty in the mailItem so they are not visible. Initially this had worked when appending the "USERINPUT" to the end of the email.... until we encountered emails exceeding 32,767 the max allowable characters. In this case the above rule will not process..... So then I appended the "USERINPUT" to the front of the email which works fine .....however, I feel this is a sloppy method.

Code:
  'Input Report Specifications to end of email
    myText = "<font size= 1><font color=""White"">‡" _
    & Me.cboLOB & "‡" _
    & Me.cboSubLOB & "‡" _
    & Me.cboDelType _
    & "</font></font size>"
My Issue is that I want to perform a rule that is like this:

Apply the rule after I send the message when UserProperty("XYZ") Exists and on this computer only move a copy to the Deliverable Folder

I tried to do this with the following code. However, it freezes the Application when I have large attachments.
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim fld As UserProperty
Dim saveFldr As Folder
Dim cpyMail As MailItem

Set saveFlder = Application.GetNamespace("MAPI").Folders("HR Data Requests").Folders("New and Open Requests").Folders("Deliverable Tracking")

For Each fld In Item.UserProperties
    If fld.Name = "CDTReporting" Then
  
    Set cpyMail = Item.Copy
    cpyMail.Move saveFldr
  
    End If
  
Next fld

End Sub
I was wondering if there is a better way to accomplish this task....seeing as this issue doesn't seem to occur if I use an outlook rule. So I guess I am trying to figure out the code that the Outlook Rule is using to move the copy....seeing as this works without issue.


Here is my UserForm Code:
Code:
Private Sub cmdSendMail_Click()

    Dim myText      As String
    Dim cpyFolder   As Outlook.Folder
    Dim sntFolder   As Outlook.Folder
    Dim cpy        As Object
    Dim NewMail     As MailItem
    Dim oInspector  As Inspector
    Dim myUserProperty  As UserProperty
   
    'Set Mail Object
    Set oInspector = Application.ActiveInspector
    If oInspector Is Nothing Then
        MsgBox "No active inspector item present.", vbCritical, "MetLife - CDT Tracker"
        Exit Sub
    End If
   
    'Set Mail Item
    Set NewMail = oInspector.CurrentItem
   
    'Make Sure Email is Editable
    If NewMail.Sent Then
        MsgBox "This is not an editable email"
        Exit Sub
    End If
     
    'Check for Subject Line and Recipient
    If NewMail.Subject = "" Or _
        NewMail.To = "" Then
            MsgBox "Please Enter a Subject Line or Recipient(s)", vbCritical, "MetLife - CDT Tracker"
            Exit Sub
    End If
     
     'Ensure Reporting Data has been filled in accurately
     If Me.cboLOB = "" Or _
        Me.cboSubLOB = "" Or Me.cboSubLOB = "Please Select LOB" Or _
        Me.cboDelType = "" Then
            MsgBox "Please fill in all reporting fields.  They are mandatory!", vbCritical, "MetLife - CDT Tracker"
            Exit Sub
     End If
         
    'Input Report Specifications to end of email
    myText = "<font size= 1><font color=""White"">‡" _
    & Me.cboLOB & "‡" _
    & Me.cboSubLOB & "‡" _
    & Me.cboDelType _
    & "</font></font size>"
   
     Set myUserProperty = NewMail.UserProperties.Add("CDTReporting", olText)
     myUserProperty.Value = "Test Test Test"
   
    'Add Report Tracking Info to Email
    If oInspector.IsWordMail Then
         NewMail.HTMLBody = myText & NewMail.HTMLBody
    Else
        'No object model to work with. Must manipulate raw text.
        Select Case NewMail.BodyFormat
            Case olFormatPlain, olFormatRichText, olFormatUnspecified
                NewMail.Body = myText & NewMail.Body
            Case olFormatHTML
                NewMail.HTMLBody = "<p>" & myText & "</p>" & NewMail.HTMLBody
        End Select
       
    End If
           
    Unload Me 'Unload usrfrmCDT
    NewMail.Send 'Send Active Outlook Message
           
End Sub

Thank you for your time.
 

Attachments

mrmmickle1

Member
Outlook version
Outlook 2010 64 bit
Email Account
Office 365 Exchange
Michael,

Thanks so much for the reply.

I assume this would be used like this:

Code:
  Dim UserProp As UserProperty

  Set UsrProp = NewMail.UserProperties.Find("CDTReporting")
     If Not UsrProp Is Nothing Then
        MsgBox "UserProperty Exists....."
     Exit Sub
     End If

Any insight into how to fix the underlying issue I'm facing? Or ideas of how to create another workaround without entering the reporting data directly in the email in white font? I would like to clean this process up a little if possible...
 

mrmmickle1

Member
Outlook version
Outlook 2010 64 bit
Email Account
Office 365 Exchange
Here is a little more detail about the process:

The process works like this:

1. User Opens Email
2. User types in To, Subject, Body Text
3. User attaches any applicable reports to tthe email (Attachments are not added using itemadd)
4. User Clicks on a QAT shortcut that launches my userform
5. User fills in the 3 fields and hits the send button
6. The send button fires this code Private Sub cmdSendMail_Click()
7. This Event is fired : Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Line of code that gets stuck in # 7 : cpyMail.Move saveFldr

To help further clarify I have run two tests:

1. I tried to run the code with large files attached to the message.

The code freezes on this line as expected: cpyMail.Move saveFldr
Note:
a. When the code freezes the Email is still visible.
b. The Item has not been moved to my Sent Items

2. I ran the code with no files attached (Just a regular email message containing text)
Code runs as expected no issue....

I did notice that the copy that is moved into the "Deliverable Tracking" Folder is a draft.

This leads me to believe that I am not using the correct code to get my end result.

Ex. When a email is sent from outlook it goes into the Sent Items Folder (As Unread etc...)

I would like an exact copy of this item in my "Deliverable Tracking" Folder. (Not a draft or Unread Copy)

Is this possible to accomplish using VBA Code. As I mentioned in my previous post the rule that I have put together works to accomplish this need....

Apply the rule after I send the message with ‡ in the body and on this computer only move a copy to the Deliverable Folder

However, I would like to avoid actually putting text in the email. This is why I am trying to store it in the UserProperty. Hope this helps to clarify my dilemma.

Diane and Michael thanks for looking into my issue. I appreciate the assistance. If I can add the item to the folder using ItemAdd I think this would resolve my issue. Not so familiar with the Outlook Object Library. Most of my work is done in Excel and Access. I'm attempting to branch out...but there is of course a learning curve :)


Note: The attachment in the first post is not applicable to my Outlook Issue. This was copied and pasted on accident.
 

mrmmickle1

Member
Outlook version
Outlook 2010 64 bit
Email Account
Office 365 Exchange
Diane,

I wanted to circle back to this thread and thank you for your time and effort. I have been doing a lot of regression testing at work that it really eating away my time. I should have an opportunity to test this out early next week. I'll post back with my results. After an initial look at some of the additional documentation your link provides I believe this is the way to go. :)
 
Status
Not open for further replies.
Top