Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   VBScript - load a data from a file and create a new Outlook contact! (http://www.go4expert.com/forums/vbscript-load-data-file-create-outlook-t1313/)

Jacek_ 31Aug2006 17:41

VBScript - load a data from a file and create a new Outlook contact!
 
hey guys!

I'd like to crate a vbs script which could load specific data from a file..

name, lastname, e-mail
e.g.
john, smith, world, john@world.com
and so on..

and then create a new contact in outlook application using those values.

To create a new contact i could you sth like this:
Const olContactItem = 2a
Dim objOutl, objContact
Set objOutl = WScript.CreateObject("Outlook.Application")
Set objContact = objOutl.CreateItem(olContactItem)
objContact.FirstName = "john"
objContact.LastName = "smith"
objContact.Email1Address = "john@world.com"
objContact.Save()

But i'm not able to create a procedure which could load that data from a file, could you please help me with that.

Jack

pradeep 1Sep2006 10:57

Re: VBScript - load a data from a file and create a new Outlook contact!
 
Here is a simple solution to the problem.

Code: VB

Const InpFile = "C:\contacts.csv" 'Import file
   Const olContactItem = 2a
   Dim objOutl, objContact,oTF,oFSO
  Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True)
   Set objOutl = WScript.CreateObject("Outlook.Application")
   
   Do While oTF.AtEndOfStream <> True
       sLine = oTF.ReadLine
       aLine = split(sline, ",",-1,1)
       sFname = aLine(0)
       sLname = aLine(1)
       sEmail = aLine(2)
   
       Set objContact = objOutl.CreateItem(olContactItem)
       objContact.FirstName = sFname
       objContact.LastName = sLname
       objContact.Email1Address = sEmail
       objContact.Save()
   
       Set objContact = Nothing
   Loop


Jacek_ 1Sep2006 14:04

Re: VBScript - load a data from a file and create a new Outlook contact!
 
Quote:

Originally Posted by pradeep
Here is a simple solution to the problem.

Code: VB

Const InpFile = "C:\contacts.csv" 'Import file
   Const olContactItem = 2a
   Dim objOutl, objContact,oTF,oFSO
  Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True)
   Set objOutl = WScript.CreateObject("Outlook.Application")
   
   Do While oTF.AtEndOfStream <> True
       sLine = oTF.ReadLine
       aLine = split(sline, ",",-1,1)
       sFname = aLine(0)
       sLname = aLine(1)
       sEmail = aLine(2)
   
       Set objContact = objOutl.CreateItem(olContactItem)
       objContact.FirstName = sFname
       objContact.LastName = sLname
       objContact.Email1Address = sEmail
       objContact.Save()
   
       Set objContact = Nothing
   Loop


thanks for help!
just one thing, when im trying to run it, i got an error:
"Expected end of statement" so i removed 'a' from ' Const olContactItem = 2a'
then

i got an error: "Invalid procedure call or argument" in this line:
"Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True)"

i tried to add 'Option Explicit' but then i get:
"variable is undefined: 'For Reading''"

...

dad59 17Sep2006 01:23

Re: VBScript - load a data from a file and create a new Outlook contact!
 
You need to include the library with the constant definitions the post here

http://p2p.wrox.com/archive/beginnin...2002-06/53.asp

explains.

the line you need to add goes before the <head> it should read :

<!-- METADATA TYPE=3D"typelib" FILE=3D"C:\windows\system32\scrrun.dll" -->

Note: the location of scrrun.dll may differ and is system dependant.

Dale

tomk358 7Oct2006 00:00

Re: VBScript - load a data from a file and create a new Outlook contact!
 
I'd like to do the same thing but also create some distribution lists at the same time. I am a complete newbie when it comes to this- can I drop that code (listed above) in a .wsf file and run it from the commandline?

Also, when I looked at the library to include, it seems you are including it as it would be on a webpage- I assume the syntax would be completely different in a .wsf file on a local machine, but have no clue what that syntax would be.

Any help would be greatly appreciated,

Thanks
-Tom


All times are GMT +5.5. The time now is 09:33.