0

got a little problem, I hope someone can help me.

(Outlook 2010 VBA)

this is my current code, what i need is when i click on a mail (only the mail i clicked on, not every mail in the folder/same place) it has to check if the Sender of the mail is already in my contacts or in the Addressbook 'All Users', and if it's not a one of those yet, open the AddContact window and fill in his/her information

what doesn't work yet is:

  • most important of all, it doesn't run the script when i click on a mail
  • the current check if the contact already exsist doesn't work and goes with a vbMsgBox (yes or no and response stuff) wich is not what i want/need if the contact already exsist then nothing has to happen.

I hope i gave enough information and someone can help me out here :)

Sub AddAddressesToContacts(objMail As Outlook.MailItem)
Dim folContacts As Outlook.MAPIFolder
Dim colItems As Outlook.Items
Dim oContact As Outlook.ContactItem
Dim oMail As Outlook.MailItem
Dim obj As Object
Dim oNS As Outlook.NameSpace

''don't want or need a vbBox/ask box, this is a part of the current contactcheck
''wich doesn't work and is totaly wrong :P
Dim response As VbMsgBoxResult

Dim bContinue As Boolean
Dim sSenderName As String

On Error Resume Next

Set oNS = Application.GetNamespace("MAPI")
Set folContacts = oNS.GetDefaultFolder(olFolderContacts)
Set colItems = folContacts.Items

''this selects the mail that is currently selected.
''what i want is that the sender of the new incoming mail gets added to contacts
''(ofcourse, if that contact doesn't exsist yet)
''so the new incoming mail gotta be selected.
For Each obj In Application.ActiveExplorer.Selection

If obj.Class = olMail Then
Set oContact = Nothing

bContinue = True
sSenderName = ""

Set oMail = obj

sSenderName = oMail.SentOnBehalfOfName
If sSenderName = ";" Then
sSenderName = oMail.SenderName
End If

Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")

''this part till the --- is wrong, i need someting to check if the contact (the sender)
''already exsists. Any ideas?
If Not (oContact Is Nothing) Then
    response = vbAbort
If response = vbAbort Then
    bContinue = False
End If
End If
''---------

If bContinue Then
Set oContact = colItems.Add(olContactItem)
With oContact

.Email1Address = oMail.SenderEmailAddress
.Email1DisplayName = sSenderName
.Email1AddressType = oMail.SenderEmailType
.FullName = oMail.SenderName

'.Save

oContact.Display

End With
End If
End If
Next

Set folContacts = Nothing
Set colItems = Nothing
Set oContact = Nothing
Set oMail = Nothing
Set obj = Nothing
Set oNS = Nothing
End Sub

hey, i still have a last question,

'sets the name of the contact
    Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")

    'checks if the contact exsist, if it does exit the for loop
     If Not oContact Is Nothing Then
        Exit For
     End If
End If

this checks if the name is already in contacts, i need it that it checks if the E-mailaddress is in contacts or not, can you help me with that?

i had someting like this in mind

set oSendermail = ?the e-mailaddress?

         If Not oSendermail Is Nothing Then
            Exit For
         End If
End If
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Ricje20
  • 3
  • 6
  • Define a rule which moves all incoming mails to your inbox if the sender is contained in your addressbook and then stops rule-processing. A second rule is then only called for senders not present in your addressbook. This second rule should call a VBA subroutine which automatically adds the sender to the addressbook before moving the mail to the inbox. How to define a rule is explained here: http://superuser.com/questions/174145/can-you-create-a-rule-in-outlook-to-move-all-emails-that-were-sent-to-any-distri – Axel Kemper Feb 26 '13 at 10:30
  • hey, thanks for the fast reaction, this is a task i recieved from my boss, and this has to run throughout the whole company, it realy has to Check if the sender exsists, and if it doesn't open the addContact window, if you click on a mail, and not when you recieve a new mail. i hope you can help me further :) – Ricje20 Feb 26 '13 at 10:32
  • OK. If your 1st rule has the precondition that the sender is in the addressbook, this implies that the sender exists. The rules are executed before the user can click on a mail. Do you have remaining concerns? – Axel Kemper Feb 26 '13 at 10:40
  • i see, can you give an exapmle of how i do this? ~thanks – Ricje20 Feb 26 '13 at 11:43

1 Answers1

0

A solution (including test routine) could look as follows: (assuming that we only consider external SMTP mails. Adjust the path to your contact folder and add some more error checking!)

Option Explicit

Private Declare Function GetTickCount Lib "kernel32.dll" () As Long

Sub AutoContactMessageRule(newMail As Outlook.mailItem)
    '  "script" routine to be called for each incoming Mail message
    '  This subroutine has to be linked to this mail type using 
    '  Outlook's rule assistant
    Dim EntryID As String
    Dim StoreID As Variant
    Dim mi As Outlook.mailItem
    Dim contactFolder As Outlook.Folder
    Dim contact As Outlook.ContactItem

    On Error GoTo ErrorHandler

    '  we have to access the new mail via an application reference
    '  to avoid security warnings
    EntryID = newMail.EntryID
    StoreID = newMail.Parent.StoreID

    Set mi = Application.Session.GetItemFromID(EntryID, StoreID)

    With mi
        If .SenderEmailType = "SMTP" Then
            Set contactFolder = FindFolder("Kemper\_local\TestContacts")

            Set contact = contactFolder.items.Find("[Email1Address]=" & Chr(34) & .SenderEmailAddress & Chr(34))
            If Not TypeName(contact) <> "Nothing" Then
                Set contact = contactFolder.items.Add(olContactItem)
                contact.Email1Address = .SenderEmailAddress
                contact.Email1AddressType = .SenderEmailType
                contact.FullName = .SenderName
                contact.Save
            End If
        End If
    End With

    Exit Sub

ErrorHandler:
    MsgBox Err.Description, vbCritical, "Ooops!"
    Err.Clear
    On Error GoTo 0
End Sub


Private Function FindFolder(path As String) As Outlook.Folder
'  Locate MAPI Folder.
'  Separate sub-folder using '/' . Example: "My/2012/Letters"
    Dim fd As Outlook.Folder
    Dim subPath() As String
    Dim I As Integer
    Dim ns As NameSpace
    Dim s As String

    On Error GoTo ErrorHandler

    s = Replace(path, "\", "/")

    If InStr(s, "//") = 1 Then
        s = Mid(s, 3)
    End If

    subPath = Split(s, "/", -1, 1)
    Set ns = Application.GetNamespace("MAPI")

    For I = 0 To UBound(subPath)
      If I = 0 Then
        Set fd = ns.Folders(subPath(0))
      Else
        Set fd = fd.Folders(subPath(I))
      End If
      If fd Is Nothing Then
        Exit For
      End If
    Next

    Set FindFolder = fd
    Exit Function

ErrorHandler:
    Set FindFolder = Nothing
End Function


Public Sub TestAutoContactMessageRule()
    '  Routine to test Mail Handlers AutoContactMessageRule()'
    '  without incoming mail messages
    '  select an existing mail before executing this routine
    Dim objItem As Object
    Dim objMail As Outlook.mailItem
    Dim started As Long

    For Each objItem In Application.ActiveExplorer.Selection
        If TypeName(objItem) = "MailItem" Then
            Set objMail = objItem

            started = GetTickCount()
            AutoContactMessageRule objMail

            Debug.Print "elapsed " & (GetTickCount() - started) / 1000# & "s"
        End If
    Next
End Sub
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • Thank you :) i was able to get some tricks of of this to resolve the problem. – Ricje20 Feb 26 '13 at 14:23
  • Hey, i still have a last question, i eddited my question and put the question under it.. I hope you can help me :) – Ricje20 Feb 26 '13 at 15:01
  • As written in my solution: The line of interest is contactFolder.items.Find("[Email1Address]=" & Chr(34) & .SenderEmailAddress & Chr(34)) – Axel Kemper Feb 26 '13 at 15:03
  • The drawback of your approach is that you get contacts with just a proper SMTP mail address but usually without a proper full name. One could mark automatically created contacts and edit them manually. – Axel Kemper Feb 26 '13 at 15:08