Dear den2002-ga,
Sorry for the delay in getting back to you. Information on accessing
the Windows Address Book from VBscript is surprisingly hard to come
by, which unfortunately turns out to be because the WAB itself *can't*
be accessed from script:
"There is a Win32 API for the Windows Address Book, but no COM
automation interface. You would need to write your own COM wrapper."
- Michael Harris (MVP) in
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&selm=u7TT4URyBHA.2200%40tkmsftngp02
I've confirmed this by referring to the relevant MSDN documentation.
However, all is not lost. As you're using Outlook, I would expect that
your addresses are stored in its "Contacts" folder, which in turn
passes the job of storing the e-mail addresses down to the WAB, and in
this case, altering the stored details in Outlook also affects the
WAB.
I've provided script samples to alter the address book in Outlook
here, they should be reasonably self-explanatory:
Adding a contact:
' Starts Outlook
Set myOlApp = CreateObject ("Outlook.Application")
' Creates a contact; repeat as necessary
' Other properties than these are available in Outlook help.
Set objNewContact = myOlApp.CreateItem (2) 'olContactItem
With objNewContact
.FirstName = "Joe"
.LastName = "User"
.CompanyName = "Acme Inc."
.Email1Address = "juser@acme.com"
.Save
End With
Editing a contact:
' Starts Outlook and gets the contacts folder
Set myOlApp = CreateObject ("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(10) ' olFolderContacts
' Edits the contact; repeat as many times as necessary
Set objContact = myFolder.Items ("Joe User")
' full name is the default property, so we find by that
With objContact
.Email1Address = "joeu@acme.com"
.Save
End With
Deleting a contact:
' Starts Outlook and gets the contacts folder
Set myOlApp = CreateObject ("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(10) ' olFolderContacts
' Deletes the contact, repeat as many times as necessary
Set objContact = myFolder.Items ("Joe User")
' full name is the default property, so we find by that
objContact.Delete
A full list of the properties which you can use to create and edit
contacts is available in Microsoft Outlook Help.
Additional Links:
"Automating Microsoft Outlook Using Visual Basic Scripting Edition or
Visual Basic for Applications", MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnout98/html/msdn_ouvbsvba.asp
I'll add that I've only been able to double-check these scripts
against Outlook XP, which is all I have here, but from the
documentation I believe they should work fine on Outlook 97.
If these fail to meet your requirements and you need to access the WAB
itself directly, it would be possible to develop a small program to do
this for you; however, that would probably be getting beyond the scope
of this question at this point.
If this answer isn't quite what you're looking for, please feel free
to request a clarification.
Hope this helps,
cerebrate-ga |
Clarification of Answer by
cerebrate-ga
on
31 Dec 2002 07:50 PST
Certainly I can do that. To check whether a contact exists is most
easily done by looking up that contact and catching the error if it
doesn't exist - for large address books, this is much faster than
walking through all of them - as in the following script:
' Start Outlook and get the Contacts folder
Set myOlApp = CreateObject ("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(10) ' olFolderContacts
'If an error occurs in the next statement, then the item is not found
On Error Resume Next
Set objContact = myFolder.Items ("Joe User")
' full name is the default property, so we find by that
If Err.Number <> 0 Then
If Err.Number = &H8004010F Then ' object could not be found
MsgBox "Contact does not exist."
Else
MsgBox "Unrecognised error: " & Err.Number
End If
End if
Err.Clear
On Error GoTo 0
In this script, I've just displayed a message box if the contact isn't
found. You don't say how you'd want to communicate this information
back if you're doing it remotely, but for your convenience I've
included a link to a page that, at the bottom, includes the VBScript
for e-mailing a message using both CDO (Outlook) and CDONTS (native
for Windows 2000 and above).
Hope this helps, and thank you for the complimentary comments,
cerebrate-ga
Additional Links:
"Handling VBScript Run-Time Errors", MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deconhandlingvbscriptruntimeerrors.asp
"Sending E-mail in VBS (SMTP)", ExpertsExchange
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_11016941.html
|
Request for Answer Clarification by
den2002-ga
on
25 Mar 2003 08:03 PST
a while ago you helped me out with cleaning my address book. The code
you supplied me with worked a treat in all the machines i tested it on
here. When i released it out into the field, it worked , but when the
user clicks on the 'to' button when they're sending an email, they
dont see all the address. Have you any idea's as to what happened, or
can you explain to me the difference between the contacts list and the
address book. If you dont have time i understand - this question was
closed a long time ago!
Thanks in advance,
Den
|