Link to home
Start Free TrialLog in
Avatar of Luniz2k1
Luniz2k1

asked on

Another Convert VBA code to VBS code

Now that I am able to remove Contacts with my prior question, I now have the code to create new contacts.  But I need the following code to be able to work inside VBS.

Set OOUTLOOK = New Outlook.Application
Set OCONTACT = Outlook.CreateItem(olContactItem)
With OOUTLOOK
    With OCONTACT
        .FullName = "List-1"
        .JobTitle = "Distribution List"
        .Email1Address = "someone1@somewhere.com"
        .Save
    End With
End With
With OOUTLOOK
    With OCONTACT
        .FullName = "List-2"
        .JobTitle = "Distribution List"
        .Email1Address = "someone2@somewhere.com"
        .Save
    End With
End With
Avatar of aelatik
aelatik
Flag of Netherlands image

Try this

Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = Outlook.CreateItem(olContactItem)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save
Avatar of Luniz2k1
Luniz2k1

ASKER

With only the following code in the VBS:

Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = Outlook.CreateItem(olContactItem)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save

I get the following error:

Script: C:\.........test6.vbs
Line: 2
Char: 1
Error: Object required: 'Outlook'
Code: 800A01A8
Source: Microsoft VBScript runtime error
is only a tipping error
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(olContactItem)
and changeAnd change olContactItem constant to its numeric value (i don't remember which one is).
remember as i told you in other Q, don't use constants in VBS, they means nothing to vbscript.
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(olContactItem)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(10)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save

Script: C:\.........test6.vbs
Line: 2
Char: 1
Error: Could not complete the operation. One or more parameter values are not valid.
Code: 80070057
Source: Microsoft Outlook
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(olContactItem)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save

Script: C:\.........test6.vbs
Line: 3
Char: 9
Error: Object doesn't support this property or method: 'OCONTACT.Fullname'
Code: 800A01B6
Source: Microsoft VBScript runtime error
SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save
MsgBox "Addition Of New List- Distribution Lists Complete."

Works ok, except for some reason, It only creates the last one.  Even If I have 4:

Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-3"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone3@somewhere.com"
        OCONTACT.Save
        OCONTACT.FullName = "List-4"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone4@somewhere.com"
        OCONTACT.Save
MsgBox "Addition Of New List- Distribution Lists Complete."

Only the last contact gets created.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set OOUTLOOK = CreateObject("Outlook.Application")
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-1"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone1@somewhere.com"
        OCONTACT.Save
Set OCONTACT = Nothing
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-2"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone2@somewhere.com"
        OCONTACT.Save
Set OCONTACT = Nothing
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-3"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone3@somewhere.com"
        OCONTACT.Save
Set OCONTACT = Nothing
Set OCONTACT = OOUTLOOK.CreateItem(2)
        OCONTACT.FullName = "List-4"
        OCONTACT.JobTitle = "Distribution List"
        OCONTACT.Email1Address = "someone4@somewhere.com"
        OCONTACT.Save
Set OCONTACT = Nothing
Set OCONTACT = OOUTLOOK.CreateItem(2)
MsgBox "Addition Of New List- Distribution Lists Complete."

Works Like a charm now.  Let me go over all of the responses to award points.
why don't you use for loop posted by aelatik?
Because I am not reading the data from an external list or database, so a loop wont do me any good.