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

Discussion in 'Web Design, HTML And CSS' started by Jacek_, Aug 31, 2006.

  1. Jacek_

    Jacek_ New Member

    Joined:
    Aug 31, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Here is a simple solution to the problem.

    Code:
    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 
     
  3. Jacek_

    Jacek_ New Member

    Joined:
    Aug 31, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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''"

    ...
     
  4. dad59

    dad59 New Member

    Joined:
    Sep 16, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    You need to include the library with the constant definitions the post here

    http://p2p.wrox.com/archive/beginning_asp/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
     
  5. tomk358

    tomk358 New Member

    Joined:
    Oct 6, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice