Hi all,
I have some VBScript code that I am trying to port over to VB.NET. I am receiving an error "Cast from type 'Field' to type 'String' is not valid." when calling the MapLegacyExchangeDN method. I'm thinking that the parameter
sDirName isn't being properly passed to the function. The same code works in VBScript. Can someone help, this should be an easy one but I cant figure it out! And yes, I am a VB.NET newbie!
Thanks.
Code:
Dim sLDAPServer As String = "server.domain.local"
Dim sDirName As String = "/o=DOMAIN/ou=First Administrative
Group/cn=Recipients/cn=shmoej"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objSession
Dim objFolder
Dim pubContactItems
Dim sEmailName
sDirName = MapLegacyExchangeDN(sDirName)
sDirName = RetrieveSMTP(sDirName)
sEmailName = sDirName
MsgBox("E-mail: " & sEmailName)
Error_Handler:
If Err.Number <> 0 Then
MsgBox("Error number: " & (Err.Number) & ", " & Err.Description)
Err.Clear()
Else : MsgBox("Everything OK")
End If
End Sub
Private Function RetrieveSMTP(ByVal sDirName As String)
Dim objMailbox As Object
Dim sMemberAddr
objMailbox = GetObject("LDAP://" & sLDAPServer & "/" & sDirName)
If Err.Number = 0 Then
sMemberAddr = objMailbox.GetEx("mail")(0)
If Err.Number = 0 Then
End If
End If
RetrieveSMTP = sMemberAddr
End Function
Private Function MapLegacyExchangeDN(ByVal sDirName As String)
Dim ADOconn
Dim strADOQueryString
Dim RS
Dim sResult
Dim bShowAll
ADOconn = CreateObject("ADODB.Connection")
ADOconn.Provider = "ADSDSOObject"
ADOconn.Open("Active Directory Provider")
strADOQueryString = "<LDAP://" & sLDAPServer & ">;(legacyExchangeDN=*" & sDirName & "*);distinguishedName;subtree"
On Error Resume Next
RS = ADOconn.Execute(strADOQueryString)
If Err.Number <> 0 Then
MsgBox("Error searching for legacyExchangeDN " & Err.Number & " " & Err.Description)
End If
sResult = ""
If Not RS.EOF Then
If RS.recordcount > 1 Then
bShowAll = True
Else
bShowAll = False
End If
While Not RS.EOF
sResult = RS.Fields(0)
RS.MoveNext()
End While
End If
RS.Close()
RS = Nothing
ADOconn = Nothing
MapLegacyExchangeDN = sResult
End Function
I have some VBScript code that I am trying to port over to VB.NET. I am receiving an error "Cast from type 'Field' to type 'String' is not valid." when calling the MapLegacyExchangeDN method. I'm thinking that the parameter
sDirName isn't being properly passed to the function. The same code works in VBScript. Can someone help, this should be an easy one but I cant figure it out! And yes, I am a VB.NET newbie!
Thanks.
Code:
Dim sLDAPServer As String = "server.domain.local"
Dim sDirName As String = "/o=DOMAIN/ou=First Administrative
Group/cn=Recipients/cn=shmoej"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objSession
Dim objFolder
Dim pubContactItems
Dim sEmailName
sDirName = MapLegacyExchangeDN(sDirName)
sDirName = RetrieveSMTP(sDirName)
sEmailName = sDirName
MsgBox("E-mail: " & sEmailName)
Error_Handler:
If Err.Number <> 0 Then
MsgBox("Error number: " & (Err.Number) & ", " & Err.Description)
Err.Clear()
Else : MsgBox("Everything OK")
End If
End Sub
Private Function RetrieveSMTP(ByVal sDirName As String)
Dim objMailbox As Object
Dim sMemberAddr
objMailbox = GetObject("LDAP://" & sLDAPServer & "/" & sDirName)
If Err.Number = 0 Then
sMemberAddr = objMailbox.GetEx("mail")(0)
If Err.Number = 0 Then
End If
End If
RetrieveSMTP = sMemberAddr
End Function
Private Function MapLegacyExchangeDN(ByVal sDirName As String)
Dim ADOconn
Dim strADOQueryString
Dim RS
Dim sResult
Dim bShowAll
ADOconn = CreateObject("ADODB.Connection")
ADOconn.Provider = "ADSDSOObject"
ADOconn.Open("Active Directory Provider")
strADOQueryString = "<LDAP://" & sLDAPServer & ">;(legacyExchangeDN=*" & sDirName & "*);distinguishedName;subtree"
On Error Resume Next
RS = ADOconn.Execute(strADOQueryString)
If Err.Number <> 0 Then
MsgBox("Error searching for legacyExchangeDN " & Err.Number & " " & Err.Description)
End If
sResult = ""
If Not RS.EOF Then
If RS.recordcount > 1 Then
bShowAll = True
Else
bShowAll = False
End If
While Not RS.EOF
sResult = RS.Fields(0)
RS.MoveNext()
End While
End If
RS.Close()
RS = Nothing
ADOconn = Nothing
MapLegacyExchangeDN = sResult
End Function