Extracting only one recipient from Msgitem.To

Status
Not open for further replies.

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I have a script that saves emails in msg format in a specified filenaming format that includes Msgitem.to. At present if an email has been sent to multiple 'To' recipients the value of then my filename includes all the To recipients. I would like to change this to only include the first To recipient. Is there any way to do this?
 
Sorry should have said that MsgItem is a Outlook Mail Item
 
Thanks for the guidance Michael. I was hoping to use code something like that below to check whether there is more than one recipient and if so then set the Msgitemto string to something slightly different than if there was only one recipient. The code below seems to fail on the second line. How can I count the number of items in the array?

Code:
arrMsgTo = Split(msgItem.To, ";")
If arrMsgTo.Count = 1 Then
   msgItemTo = msgItem.To
   Else
   Set msgItemTo = arrMsgTo(1)
End If
 
I also tried using Ubound and Lbound to try and calculate number of elements in array and that also failed
 
I think I've sorted it using:

Code:
arrMsgTo = Split(msgItem.To, ";")
arrcount = UBound(arrMsgTo) - LBound(arrMsgTo) + 1
If arrcount = 1 Then
   msgItemTo = msgItem.To
   Else
   msgItemTo = LBound(arrMsgTo)
End If

I also had to set the arrMsgTo using
Code:
Dim arrMsgTo() As String
 
Error in code above as it was not setting the msgItemTo to the value of the first recipient. The correct code is below:
Code:
arrMsgTo = Split(msgItem.To, ";")
arrcount = UBound(arrMsgTo) - LBound(arrMsgTo) + 1
If arrcount = 1 Then
    msgItemTo = msgItem.To
    Else
    msgItemTo = arrMsgTo(0)
End If
 
Even if there's only one recipient, Split returns an array with one item, so you can always use arr(0) and don't need the If-Else thing.
[DOUBLEPOST=1447138107][/DOUBLEPOST]Even if there's only one recipient, Split returns an array with one item, so you can always use arr(0) and don't need the If-Else thing.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
B Extracting email addresses from a folder - how to also get the name of the person the address is for? Using Outlook 5
O Extracting all mail addresses from all folders Using Outlook 10
M Extracting cell data from table Outlook VBA and Custom Forms 2
Z Default VBA code for extracting data from email (Outlook) to Excel? Outlook VBA and Custom Forms 1
V Extracting user-defined details from a public folder Outlook VBA and Custom Forms 2
M Oh please read---> Extracting email web leads on to excel spread sheet Using Outlook 3
J Need Help Extracting Email Content into Excel Specifically the Attachment Name Using Outlook 3
D Extracting Location info from All Day Event in mailboxes into webpage Exchange Server Administration 3
E Extracting CC Email Address Using VBA Outlook VBA and Custom Forms 2
K Extracting Calendar information from a OST file? Exchange Server Administration 5
F Graphics in email / Mac recipient garbled Using Outlook 0
T Outlook 2010 recipient no longer shows in 'Send To' Using Outlook 0
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
C Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 4
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
B VBScript doesn't run on Recipient Email Outlook VBA and Custom Forms 2
T "Words In Recipient's Address" Rule Causes Outlook To Stop Processing Rules Using Outlook 3
M ERROR: None of your email accounts could send to this recipient Using Outlook 2
N Custom Form Controls Not Visible To Recipient Outlook VBA and Custom Forms 3
Daniel Schunk User-defined form arrives empty at the recipient Using Outlook 3
B Looking to get the Recipient email address (or even the "friendly name") from an email I am replying to using VBA Outlook VBA and Custom Forms 4
D Moving Emails Based on Recipient/Sender Outlook VBA and Custom Forms 4
J Signatures that contain recipient's email address Outlook VBA and Custom Forms 7
broadbander Needing help with reply/reply all while keeping attachments and adding a new CC recipient. Outlook VBA and Custom Forms 5
L Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 33
M recipient cache? Using Outlook 2
N Importing Google contacts from CSV file removes recipient names in autocomplete list Using Outlook 0
H How to show recipient email address? Using Outlook 0
R OL2010 - Send to Mail Recipient going to wrong account Using Outlook 1
J Searh For Recipient Email address Outlook VBA and Custom Forms 1
Diane Poremsky Display the Recipient Email Address in the Sent Items Folder Using Outlook 0
E Meeting reminders are set for the recipient Exchange Server Administration 9
E Send a Reminder/Task to certain Email Recipient Using Outlook 5
B Recipient of a forwared message getting multiple emails Using Outlook 2
D Outlook VBA to open Excel attachment and send recipient's email address to a workbook cell? Using Outlook 4
E Automatically pick up Recipient's Name on the Body of the Email Message Using Outlook 1
Aussie Looking for Outlook macro to Copy Recipient Names into Email Body Outlook VBA and Custom Forms 3
L Outlook underlining all text after being sent to recipient Using Outlook 3
W Change Template Recipient Automatically - Outlook 2007 Using Outlook 5
L Contact Form - Recipient Control Field Using Outlook 2
F Seeing the recipient in sent emails Using Outlook 1
J 2010 appointment for recipient shows as mail Using Outlook 3
K TextBox on Custom Form Page :: Passing value from sender to the recipient Outlook VBA and Custom Forms 1
K Recipient's name replaced by open and closed brackets! Using Outlook 2
R POP3 Mail repeating while sending to group of recipient Using Outlook 1
J Outlook messagebox with name recipient Using Outlook 1
J Recipient email address doesn't get underlined Using Outlook 4

Similar threads

Back
Top