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: 381
  • Last Modified:

Outlook Message Contacts

Hi,

I would like to create a script that will allow me to set the "Contacts" property in an Outlook message to a specific text string.

Please note I am not looking to work with Outlook Contacts in any way, I am taking about the field labelled "Contacts..." if you choose message properties (options in pre-2010 versions) of an e-mail message.

Thanks,
Kevin
0
kevinmcallister
Asked:
kevinmcallister
  • 4
  • 3
1 Solution
 
David LeeCommented:
Hi, Kevin.

The Contacts property is intended to be a link to a contact item.  What is that you want to put in the field and will it be the same string for every message?  If not, then how should the script determine what to enter in the field?
0
 
kevinmcallisterAuthor Commented:
It would be the same for every message, yes. I know that if I do this by hand, I can type in the contact name and it accepts what I have typed in.

Thanks in advance for any help you can offer.

Kevin
0
 
David LeeCommented:
Something like this will do it.  This would add a link to a contact with the name of John Doe to all outbound emails.

Follow these instructions to add the code to Outlook 2007 (I don't have a set of instructions for 2010 yet).

1.  Start Outlook
2.  Click Tools->Macro->Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
5.  Edit the code as needed.  I included comment lines wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Click Tools > Trust Center
9.  Click Macro Security
10. Set Macro Security to "Warnings for all macros"
11. Click OK
12. Close Outlook
13. Start Outlook.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Say yes.


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim olkLink As Outlook.Link, olkCon As Outlook.ContactItem
    If Item.Class = olMail Then
        'On the next line change the full name of the contact to link to'
        Set olkCon = Session.GetDefaultFolder(olFolderContacts).Items.Find("[FullName] = 'John Doe'")
        If TypeName(olkCon) <> "Nothing" Then Set olkLink = Item.Links.Add(olkCon)
        Item.Save
    End If
End Sub

Open in new window

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
kevinmcallisterAuthor Commented:
This is great! Thanks.

Sorry to be a pain, but can you advise if this would work for a message I have already received which has a contact in it, but which I want to change to, say, John Doe?

Thanks again so much for your help!
Kevin
0
 
David LeeCommented:
You're welcome!

No pain.  This script only works for messages that are being sent.  I've created another script (below) that's a knock-off of the first script and will work for message in any Outlook folder.  Simply add this code to what you already have.  To use it

1.  Select one or more messages in any folder.
2.  Run the macro.
Sub SetMessageContact()
    Dim olkMsg As Object, olkLink As Outlook.Link, olkCon As Outlook.ContactItem
    'On the next line change the full name of the contact to link to'
    Set olkCon = Session.GetDefaultFolder(olFolderContacts).Items.Find("[FullName] = 'John Doe'")
    For Each olkMsg in Application.ActiveExplorer.Selection
        If olkMsg.Class = olMail Then
            If TypeName(olkCon) <> "Nothing" Then Set olkLink = olkMsg.Links.Add(olkCon)
            Item.Save
        End If
    Next
    Set olkMsg = Nothing
    Set olkLink = Nothing
    Set olkCon = Nothing
End Sub

Open in new window

0
 
kevinmcallisterAuthor Commented:
Thanks so much. I feel really guilty about this, but it's giving me an error on the line

Item.Save

Any ideas? I have spent most of the morning poking round fora online trying to see if I could resolve it myself, but I'm getting nowhere!

Thanks again.
Kevin
0
 
David LeeCommented:
That's my fault.  Because of the way this routine needs to work I changed a variable name and missed changing that one occurrence.  Line 8 should read

    olkMsg.Save
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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