Script For Add Contacts

Dear All,

I have this Script that is Add only One Contact to the Contact Folder in Personal folder in MS-Outlook 2003 .
----------------------------
Const olContactItem = 2

Set objOutlook = CreateObject("Outlook.Application")
Set objContact = objOutlook.CreateItem(olContactItem)

objContact.FullName = "Ken Myer"
objContact.Email1Address = "kenmyer@fabrikam.com"
objContact.CompanyName = "Fabrikam"
objContact.JobTitle = "Network Administrator"
objContact.HomeTelephoneNumber = "555-555-8888"
objContact.HomeAddress = "3725 205th NE" & vbCrLf & "Redmond, WA 98052"
objContact.Birthday = "9/15/1966"
Objcontact.WebPageaddress = "http://www.soma.com"

objContact.Save
------------------------------------------------------------------------------------------------------------------------------

Now , My question is need your opinion.
Our Company Consist of 3 Office as the Following :-

1- Office In Saudi Arabia called Head-Office------ have 40 users
2- Office in Egypt- Called Egypt Office.------------ have 30 Users.
3- Office in Jordan-Called Jordan-Office.---------- Have 20 Users.

Now,

The Users in Head Office, have 3 Folders under Contact folder, as the Following:-

1- Head-office.
2- Egypt-Office.
3- Jordan-Office.

and it have the contacts for all employees according to the office.

Now, we have a problem.

if we Added a Normal user to any Office, we have to write a paer for each department and send it by fax and this Paper include the User Details.

Now , in this script, its Add user Only to the Contact folder only .

I want to write a script for example to the Users of Head Office only.
So, if any Persone came new and hired in Head Office , i need to write a script to add all of His Information to Head-Office Folder only Not in other Folder .

and the same way for other folders .

So Please help me to write a script to add user XXX to this Folder which is Named ( Head Office ) only .

THis is Urgent Matter .
 



Now each one of this user have his Personal Folder on His PC.

we create 3 Folder under Contact Folder and we named as the following:-

1. Egypt-Supplier.
2. Jordan-Supplier.
3. UK-Supplier.


Now, if we recevied any New Contact infomation, we have to run and make it on 400 PC.

Now , i am thinking  to have script and run on
rolamohammedAsked:
Who is Participating?
 
paulgrunnerConnect With a Mentor Commented:
Use the following at the beginning of your code instead of using [Set objContact = objOutlook.CreateItem(olContactItem)]

    Dim objContactsFolder As MAPIFolder
    Set objContactsFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)
   
    Dim objOfficeSubFolder As MAPIFolder
    Set objOfficeSubFolder = objContactsFolder.Folders.Item("OfficeName")
   
    Dim objContact As ContactItem
    Set objContact = objOfficeSubFolder.Items.Add(olContactItem)
0
 
rolamohammedAuthor Commented:

Thanks for your reply.

But its Not working and gives me this error :-

Script: c:\......................
Line : 3
Char : 23
error : Excepected end of statement.
code : 800A0401
Source: Microsoft VB Script Compilanation.

===============

this is the script  after modification .
-----------------------------
Const olContactItem = 2
Set objOutlook = CreateObject("Outlook.Application")
Dim objContactsFolder As MAPIFolder
Set objContactsFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)  
Dim objOfficeSubFolder As MAPIFolder
Set objOfficeSubFolder = objContactsFolder.Folders.Item("OfficeName")
Dim objContact As ContactItem
Set objContact = objOfficeSubFolder.Items.Add(olContactItem)
objContact.FullName = "Ken Myer"
objContact.Email1Address = "kenmyer@fabrikam.com"
objContact.CompanyName = "Fabrikam"
objContact.JobTitle = "Network Administrator"
objContact.HomeTelephoneNumber = "555-555-8888"
objContact.HomeAddress = "3725 205th NE" & vbCrLf & "Redmond, WA 98052"
objContact.Birthday = "9/15/1966"
Objcontact.WebPageaddress = "http://www.sex-orgabi.com"

objContact.Save

0
 
paulgrunnerCommented:
Have you add reference to Microsoft Outllook? (Using Tools -> Reference)
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
rolamohammedAuthor Commented:

NO, can you tell me how i write ?
0
 
paulgrunnerCommented:
If you're using VB there's a menu call Tools, under it you can find the menu item: Reference.
You then have to find and select "Microsoft Outlook..."

If you're using VB script, I think you can just write:
Dim objContactsFolder
(or Dim objContactsFolder As Object).

Do the same for lines 5, 7.

0
 
rolamohammedAuthor Commented:

when i open the Outlook , there is no any thing like taht
0
 
paulgrunnerCommented:
Are you using the Visual Basic Editor inside Outlook (under Tools -> Macro menu)?
If not, what are you using?
0
 
rolamohammedAuthor Commented:
guys,

let us start from the Beginig .

first of all, let me review this with you .

1- they said to me i have to Add refrence to Microsoft outlook ( using Tools --refrence ).

when i click on Tools on Microsoft , there is no any refrence to that.

But i went to tools - Macro Menu & then i found .


so, my question is , how should i do that ? should i write it there or not ?



0
 
rolamohammedAuthor Commented:
guys , really i do not know what should i do , please confirm with me and let me do it for my self .
0
 
rolamohammedAuthor Commented:

the first thing i made, i opened the Notebad , and i wrote the Script in TXT and then i save it VBS formate.

the script was for Add Contact to His Personal Folder and its worked without any Problem.

But when i am trying to Write the same script to add some of users to specific Personal Folder , i found that , its gives this error message :-

Script: c:\......................
Line : 3
Char : 23
error : Excepected end of statement.
code : 800A0401
Source: Microsoft VB Script Compilanation


Also, what i did is i wrote this script in Note PAD , and then save it in VBS  and i got this Error message . so where is the wrong exactly located ? can you please confirm with me .

they said to me , i have to use following at the beginning of your code instead of using [Set objContact = objOutlook.CreateItem(olContactItem)]

    Dim objContactsFolder As MAPIFolder
    Set objContactsFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)
   
    Dim objOfficeSubFolder As MAPIFolder
    Set objOfficeSubFolder = objContactsFolder.Folders.Item("OfficeName")
   
    Dim objContact As ContactItem
    Set objContact = objOfficeSubFolder.Items.Add(olContactItem)

=============================================================================
here is the script after what they said to me :-

Const olContactItem = 2
Set objOutlook = CreateObject("Outlook.Application")
Dim objContactsFolder As MAPIFolder
Set objContactsFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)  
Dim objOfficeSubFolder As MAPIFolder
Set objOfficeSubFolder = objContactsFolder.Folders.Item("OfficeName")
Dim objContact As ContactItem
Set objContact = objOfficeSubFolder.Items.Add(olContactItem)
objContact.FullName = "Ken Myer"
objContact.Email1Address = "kenmyer@fabrikam.com"
objContact.CompanyName = "Fabrikam"
objContact.JobTitle = "Network Administrator"
objContact.HomeTelephoneNumber = "555-555-8888"
objContact.HomeAddress = "3725 205th NE" & vbCrLf & "Redmond, WA 98052"
objContact.Birthday = "9/15/1966"
Objcontact.WebPageaddress = "http://www.sex-orgabi.com"

objContact.Save
=============================================================================

so where is the Problem Located on ? can you please confirm ?




0
 
paulgrunnerCommented:
The code below is tested !

Const olContactItem = 2
Const olFolderContacts = 10
Set objOutlook = CreateObject("Outlook.Application")
Set objContactsFolder = objOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)  
Set objOfficeSubFolder = objContactsFolder.Folders.Item("OfficeName")
Set objContact = objOfficeSubFolder.Items.Add
objContact.FullName = "Ken Myer"
objContact.Email1Address = "kenmyer@fabrikam.com"
objContact.CompanyName = "Fabrikam"
objContact.JobTitle = "Network Administrator"
objContact.HomeTelephoneNumber = "555-555-8888"
objContact.HomeAddress = "3725 205th NE" & vbCrLf & "Redmond, WA 98052"
objContact.Birthday = "9/15/1966"

objContact.Save




Please note:
Usually, my code might have syntax errors. This is because I don't always have the time to test it, and I assume that you can fix those syntax errors.
I'm sorry that the previous code didn't work, but the solution idea is what mattered.
The code above should work. If not, contact me again.
0
 
rolamohammedAuthor Commented:

agian , its not working and its display this error message :-


script: medo-2.vbs
line : 5
char :1
Error : the operation failed . an Object could not be found.
Code : 800401F
source : Microsoft Outlook


========

please i take a copy from it and i past on Notpad and then save it in .VBS .

please help me to solve this issue , as soon as possible .

the Idea which i want to do it, is i want to add for example 6 contacts in specific Folder uneder the contacts .

so please help me to do it.


0
 
paulgrunnerCommented:
Run outlook at the background and try again.
If you don't want Outlook needed to be open, let me know.
0
 
rolamohammedAuthor Commented:
what do you mean by Run the Outlook at background ?

the outlook was opened and i run this scrpit .

do you mean to close the softeware of outlook and run this script?
0
 
rolamohammedAuthor Commented:
i close it and i run the script and it gives me the same error .

please help.
0
 
paulgrunnerCommented:
It will take me few hours to get back to you.
Please be patient.
0
 
paulgrunnerCommented:
Change "OfficeName" to the name of the office you need, e.g.: "Head-office".
Make sure you have a sub folder under Contacts folder in outlook with the same name ("Head-office")
0
 
rolamohammedAuthor Commented:
Yes Man,

its Workinggggggggggggggggggggggggggggggggggggggggg


many thanks for youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu


many thanks for you ,


but if i have for example 20 users i want to add them in the Head office only folder , please tell me how can i do it ,


Urgent , urgent .


Many thanks For you , Many thanks For you ,

Many thanks For you , Many thanks For you ,

Many thanks for You ,

Many thanks For you

Please complete your good work by tell me How i will do for 20 contacts in Head-Office folder ?


please .


0
 
rolamohammedAuthor Commented:

I would like to thank you for your help .

i update my supervisor of it , he like the IDEA tooooooo much and he is happy with your Help.

Man,

i want to adjust it something from you .

First , let us suppose have 40 Perosons and we have there contacts .

I want to run or write a script to Add the 40 Contacts in this Folder only .

So Please can you help me and try to do your best with helping me to write a same script but for only 40 Persons ?

and which one is good idea , is to make the script to read from Excel Sheet only , so i can ask for example my Assistant to put all the 40 People in one excel sheet and then we will write a script to take all the Data From Excel sheet and then Put it in Personal Folder called " head -office "  ? or to have the script to include 40 contacts ?

Please help me to do it .


many many many thanks for you & for your Help .
0
 
paulgrunnerCommented:
Expert Exchanges allow only one question per post.
Your original question was about how to add a contact to Outlook contacts using VB script.
I believe I answered this question.

If you want to ask another question about writing a script that read excel file, you should post another question, and define the structure of you excel file, and what exactly do you want to do with it.

Glad to be at your help,
paul
0
 
rolamohammedAuthor Commented:

I am so soory, if i made this, but i will Post another question.

its really wonderful that , you help me like this, many thanks for you & for your Help .

its really good solution from you and i said that , because you helped me toooooo much.

Please allow me to post another 3 questions regarding it .

thanks .

Rola Mohammed.
0
 
rolamohammedAuthor Commented:
to Mr. paulgrunner

Please, i need your help.

I added some question regarding the new script ( script with Excel ) but still iam loosing .

please see it .
0
 
rolamohammedAuthor Commented:

Mr. Paulgurnner,

Here is the script, this script i need it to take the Contact information from Excel sheet, and i want it to store in Specfic folder called " head-office ".

but when i run this script , i found that , it gives me this error message.

Line : 1
Char: 11
Error : Expected end of statement.
Code : 800A0401
Source : Microsoft VBSCRIPT compliation Error

so please tell me where is the error exactly .

i made every line in the script numbered , so please review and tell me where is the Problem is ?

--------------------------------------


1- Dim objNS As Object, oOlFolder As Object
2- Set objOutlook = CreateObject("Outlook.Application")
3- Set objNS = objOutlook.GetNamespace("MAPI")
4- Set oOlFolder = objNS.Folders("Contacts").Folders("Head-office")

5- Const olContactItem = 2


6- Set objExcel = CreateObject("Excel.Application")
7- Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\contacts.xls")

x = 1

8- Do Until objExcel.Cells(x, 1).Value = ""

    9- Set objContact = objOutlook.CreateItem(olContactItem)
   10- objContact.FullName = objExcel.Cells(x, 1).Value
    11- objContact.CompanyName = objExcel.Cells(x, 2).Value
   12-  objContact.Email1Address = objExcel.Cells(x, 3).Value
   13-  objContact.Save

    x = x + 1
14- Loop

15- objExcel.Quit

16- Set myItems = oOlFolder.Items
17- Set objContact = myItems.Add(olContactItem)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.