Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find a contact in Outlook using VBA

3,088 views
Skip to first unread message

PW

unread,
Jan 24, 2013, 7:50:44 PM1/24/13
to
Hi,

I wrote a routine to add a contact to Outlook. But if the first and
last name already exists, it will write the same record again.

Is there a way in VBA to check if the first and last name exists
(rather than looping through all the contacts)?

Thanks,

-paulw

PW

unread,
Jan 24, 2013, 11:03:15 PM1/24/13
to
I figured it out! Wow! :-)

-paulw

The Frog

unread,
Jan 25, 2013, 4:10:51 AM1/25/13
to
On Thu, 24 Jan 2013 21:03:15 -0700, PW
<emailad...@ifIremember.com> wrote:
> I figured it out! Wow! :-)

And how did you do it?

--
Cheers

The Frog

PW

unread,
Jan 25, 2013, 12:29:01 PM1/25/13
to
On Fri, 25 Jan 2013 20:10:51 +1100, The Frog
<Mr.frog...@googlemail.com> wrote:

>On Thu, 24 Jan 2013 21:03:15 -0700, PW
><emailad...@ifIremember.com> wrote:
>> I figured it out! Wow! :-)
>
>And how did you do it?

Like this! I need to also search for the first name. More fun with
Access's syntax ;-)

Dim olApp As New Outlook.Application
Dim olContact As Outlook.ContactItem
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder

Dim strFirstName As String
Dim strLastName As String
Dim blnContinue As Boolean
Dim varReturnVal As Variant

strFirstName = Me.txtFirstName
strLastName = Me.txtLastName

Set olContact = olApp.CreateItem(olContactItem)
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderContacts)

blnContinue = True

If Not IsNull(strFirstName) And Not IsNull(strLastName) Then

Set olContact = olFolder.Items.Find("[lastname] = " & strLastName)

If Not TypeName(olContact) = "Nothing" Then 'Match found

varReturnVal = MsgBox("A record with the same first and last
names already exists in Outlook. Continue?", vbOKCancel, "Duplicate
name")

If varReturnVal = vbCancel Then
blnContinue = False
End If

End If

End If

.
.
.
.

PW

unread,
Jan 25, 2013, 12:44:25 PM1/25/13
to
Set olContact = olFolder.Items.Find("[lastname] = '" & strLastName
& "' And [firstname] = '" & strFirstName & "'")


On a roll! :-)

The Frog

unread,
Jan 29, 2013, 5:39:25 AM1/29/13
to
Thankyou for sharing. Much appreciate the assist.

--
Cheers

The Frog

PW

unread,
Jan 29, 2013, 12:48:35 PM1/29/13
to
On Tue, 29 Jan 2013 21:39:25 +1100, The Frog
<Mr.frog...@googlemail.com> wrote:

>Thankyou for sharing. Much appreciate the assist.

This is fun stuff! Now I am trying to create calendar items, and then
maybe some tasks.

-paulw

zachs...@gmail.com

unread,
Feb 8, 2018, 9:33:59 AM2/8/18
to
Thanks PW. 5 years later this saved me quite a bit of time.
0 new messages