Need help porting code to VB.NET

M

Martin

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.

Martin

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
Dim sDirName
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
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
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
 
A

Al Reid

Martin,

You need to get out of the habit of using default properties.

Change sResult = RS.Fields(0) to sResult = RS.Fields(0).Value
and you should be in business.
 
M

Martin

Al,

Thanks for the quick reply and help! It works now! I cant believe it was
something that small.

-Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top