Outlook - Setting Contact attributes using CallByName - Value not fall within range

RGM

Member
Joined
Nov 6, 2012
Messages
6
Programming Experience
Beginner
LS.


I'm trying to update Outlook Contact attributes using CallByName, this because I do not know which attribute I will be setting at runtime. The attribute name (e.g. LastName) is passed to the function as a string as is the value it will be getting.

for all combinations of attributes and values I've tried I'm getting the error:
"Value does not fall within the expected range"

All combinations are valid when aplied manually to the same contact.

The same method is allowing me to get values but not to set values.

Anyone an idea?

The failing code that gives the "Value does not fall within the expected range" at line 14 (CallByName)

    Function UpdateContact(ByVal objOl As Outlook.Application, ByVal ID As String, strName As String, strNewValue As String) As String
 
        Dim objNS As Outlook.NameSpace
        Dim objContact As Outlook.ContactItem
        Dim objTemp As Object
 
        objNS = objOl.GetNamespace("MAPI")
        For Each objTemp In objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items
            'Check whether the object is actually a Contact as there can be other stuff in Outlook folders
            objContact = TryCast(objTemp, Outlook.ContactItem)
            If Not objContact Is Nothing Then
                If objContact.EntryID = ID Then
                    'MessageBox.Show("Contact Found: " & objContact.FirstName & " - " & objContact.LastName)
                    CallByName(objContact, strName, CallType.Set, strNewValue)
                    Return "0"
                End If
            End If
        Next
 
        Return "1"
 
    End Function 'UpdateContact


vb.net 4.0, Outlook 2010, VS 2012.

Thank you in advance!

Best regards, Ruud.
 

RGM

Member
Joined
Nov 6, 2012
Messages
6
Programming Experience
Beginner
found it: Let vs Set

Ok, figured it out, instead of using CallType.Set I'm now using CallType.Let.

The reference documentation does not mention the possible use of Calltype.let as far as I can find:
Interaction.CallByName Method (Microsoft.VisualBasic)
Calling a Property or Method Using a String Name (Visual Basic)

The working function that lets me set an Outlook Contact's property using a String:

Function UpdateContact(ByVal objOl As Outlook.Application, ByVal ID As String, strName As String, strNewValue As String) As String

Dim objNS As Outlook.NameSpace
Dim objContact As Outlook.ContactItem
Dim objTemp As Object

objNS = objOl.GetNamespace("MAPI")
For Each objTemp In objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items
'Check whether the object is actually a Contact as there can be other stuff in Outlook folders
objContact = TryCast(objTemp, Outlook.ContactItem)
If Not objContact Is Nothing Then
If objContact.EntryID = ID Then
CallByName(objContact, strName, CallType.Let, strNewValue)
objContact.Save()
Return "0"
End If
End If
Next

Return "1"

End Function 'UpdateContact


Best regards,

Ruud.
 
Last edited:
Top Bottom