Sign in to follow this  
Followers 0
inthewayboy

Outlook Com Scripting

4 posts in this topic

All of our users run Outlook 2003 with a local PST, no Exchange server at all. Configuration and backup is done via other scripts, and now I'm trying to get past a contacts issue. I've only just learned about COM as of yesterday, so I'm very new at it. Using some vbs examples I've managed to create a script that will use an Excel file to load in new contacts, and it does this perfectly.

However, the hurdle is trying to minimize duplicates. Currently it just adds new contacts, so if one already exists then it just adds another one. If I run the import script twice, then I get two sets of the contacts. In the examples I was referencing there is a section where the code checks to see if a contact exists. It uses the TypeName function, which for AutoIt is the ObjName function. However, I can't get any syntax to work. It will work the same if the contacts exists or not. Here's what I'm trying to run:

If ProcessExists ( "OUTLOOK.EXE" ) Then
    ProcessClose ( "OUTLOOK.EXE" )
    Sleep ( 5000 )
    EndIf

Run ( @ProgramFilesDir & "\Microsoft Office\OFFICE11\OUTLOOK.EXE" )

Sleep ( 5000 )

$objOutlook = ObjCreate ( "Outlook.Application" )

$objExcel = ObjCreate ( "Excel.Application" )
$objWorkbook = $objExcel.Workbooks.Open ( @ScriptDir & "\contacts.xls" )

$xxx = 2

Do 

$objContact = $objOutlook.CreateItem ( 2 )
$objContact.LastName = $objExcel.Cells($xxx,1).Value
$objContact.FirstName = $objExcel.Cells($xxx,2).Value
$objContact.FullName = $objExcel.Cells($xxx,3).Value
;$objContact.User = $objExcel.Cells($xxx,4).Value
$objContact.BusinessTelephoneNumber = $objExcel.Cells($xxx,5).Value
$objContact.MobileTelephoneNumber = $objExcel.Cells($xxx,6).Value
$objContact.HomeTelephoneNumber = $objExcel.Cells($xxx,7).Value
$objContact.BusinessFaxNumber = $objExcel.Cells($xxx,8).Value
$objContact.Department = $objExcel.Cells($xxx,9).Value
$objContact.JobTitle = $objExcel.Cells($xxx,10).Value
$objContact.ManagerName = $objExcel.Cells($xxx,11).Value
$objContact.Email1Address = $objExcel.Cells($xxx,12).Value
$objContact.Email2Address = $objExcel.Cells($xxx,13).Value
$objContact.OfficeLocation = $objExcel.Cells($xxx,14).Value
$objContact.Save

$xxx = $xxx + 1

Until $objExcel.Cells($xxx,1).Value = ""

$objExcel.Quit

$objOutlook.Quit

And here is the item checking code, in vbs:

Set olApp = CreateObject("Outlook.Application")
    Set objNameSpace = olApp.GetNamespace("MAPI")
    Set objContacts = objNameSpace.GetDefaultFolder(10)
    Set objContact = objContacts.Items.Find("[FileAs] = ""Doe, John""")
    If Not TypeName(objContact) = "Nothing" Then
        MsgBox "yyy"
    Else
        MsgBox "nnn"
    End If

Now I've seen and tried some of the various COM error handling suggestions on the forum, but I can't figure out what I'm missing. If I can get an If/Then thing going that's all I need. From the example in vbs it looks like the error code for the value not being present is literally "Nothing", but I can't figure out what error code the ObjName function gives...again, using something like:

$OutlookApplication = ObjCreate ( "Outlook.Application" )
$OutlookNamespace = $OutlookApplication.GetNamespace("MAPI")
$OutlookContacts = $OutlookNamespace.GetDefaultFolder(10)
$OutlookContact = $OutlookContacts.Items.Find( '[FileAs] = "Doe, John"' ) 

If ObjName ( $OutlookContact ) = "" Then
     MsgBox ( 0 , "Debug" , "Error" )
Else
     MsgBox ( 0 , "Debug" , "Success" ) 
EndIf

Seems to give me the same result if the contact exists or not...help!

I'm sure like the rest of my issues someone will reply with a simple fix, I thank you in advance!

Share this post


Link to post
Share on other sites



Forgot to mention, using the latest beta (3.1.1.119)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'm sure like the rest of my issues someone will reply with a simple fix, I thank you in advance!

This script to test for the FileAs field in the Outlook addressbook works for me:

$OutlookApplication = ObjCreate ( "Outlook.Application" )
$OutlookNamespace = $OutlookApplication.GetNamespace("MAPI")
$OutlookContacts = $OutlookNamespace.GetDefaultFolder(10)
$OutlookContact = $OutlookContacts.Items.Find( '[FileAs] = "Doe, John"' ) 
If IsObj( $OutlookContact) Then
     MsgBox ( 0 , "Debug" , "Found" )
Else
     MsgBox ( 0 , "Debug" , "Not Found" ) 
EndIf
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

See...it's always the simple things :think: I didn't think to look for functions that started with something other than obj. To verify, the code you provided (Which works great!) basically checks the results of the $OutlookContact variable to see if it's an object? And if it's not a valid contact, then it's not a vaild object either, so that's sweet...thanx again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0