?
Solved

Script For Add Contacts

Posted on 2006-04-29
23
Medium Priority
?
977 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:rolamohammed
  • 14
  • 9
23 Comments
 
LVL 4

Accepted Solution

by:
paulgrunner earned 2000 total points
ID: 16568339
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
 

Author Comment

by:rolamohammed
ID: 16568426

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
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16568530
Have you add reference to Microsoft Outllook? (Using Tools -> Reference)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rolamohammed
ID: 16568706

NO, can you tell me how i write ?
0
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16569784
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
 

Author Comment

by:rolamohammed
ID: 16572743

when i open the Outlook , there is no any thing like taht
0
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16575168
Are you using the Visual Basic Editor inside Outlook (under Tools -> Macro menu)?
If not, what are you using?
0
 

Author Comment

by:rolamohammed
ID: 16584020
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
 

Author Comment

by:rolamohammed
ID: 16584033
guys , really i do not know what should i do , please confirm with me and let me do it for my self .
0
 

Author Comment

by:rolamohammed
ID: 16584115

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
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16584505
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
 

Author Comment

by:rolamohammed
ID: 16584714

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
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16584756
Run outlook at the background and try again.
If you don't want Outlook needed to be open, let me know.
0
 

Author Comment

by:rolamohammed
ID: 16584866
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
 

Author Comment

by:rolamohammed
ID: 16584902
i close it and i run the script and it gives me the same error .

please help.
0
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16585177
It will take me few hours to get back to you.
Please be patient.
0
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16585202
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
 

Author Comment

by:rolamohammed
ID: 16585390
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
 

Author Comment

by:rolamohammed
ID: 16585888

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
 
LVL 4

Expert Comment

by:paulgrunner
ID: 16589635
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
 

Author Comment

by:rolamohammed
ID: 16593558

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
 

Author Comment

by:rolamohammed
ID: 16603408
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
 

Author Comment

by:rolamohammed
ID: 16603520

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question