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

  • upload_2016-5-11_17-51-31.png
    upload_2016-5-11_17-51-31.png
    17.9 KB · Views: 640
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...
 
Does checking the specific userprop freeze it on large messages? Have you tried using itemadd and watching the sent folder for an item with that property.... the after sending rule moves a copy of the item in the sent folder.
 
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.
 
I did notice that the copy that is moved into the "Deliverable Tracking" Folder is a draft.

Exactly. This is because you're copying it before it's sent. You need to use a macro that watches the sent folder and moves it - an itemadd macro. Sample code is here - How to use an ItemAdd Macro
 
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.
Similar threads
Thread starter Title Forum Replies Date
N Get the seconds with userproperties.add method VBA Outlook Outlook VBA and Custom Forms 4
D Cannot populate certain UserProperties in Outlook from Excel Outlook VBA and Custom Forms 2
W Appointment userproperties disappear Outlook VBA and Custom Forms 4
J Storing userproperties to array Outlook VBA and Custom Forms 4
R How to search for blank userproperties field Outlook VBA and Custom Forms 4
M Assigning Values to Contact UserProperties Outlook VBA and Custom Forms 1
S UserProperties of MailItem object. Outlook VBA and Custom Forms 3
V Difference between UserProperties and ItemProperties? Outlook VBA and Custom Forms 4
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3

Similar threads

Back
Top