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: 489

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.
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
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 10
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1

Similar threads

Top