How can i split a String and use them in a Loop in VB? - vb.net

i get a string whith email adresses, seperated by a ";" which look like this:
geve#krag.de;;;Tobias#nxs.de;Wissel#weg.de;Sand#nex.de;Claudia#bea.de;;
i want to send an appointment to these email adresses here a sample to one person:
Dim appointment As New EWS.Appointment(esb)
appointment.Subject = "Einladung zu einem Termin"
appointment.Body = txtThema.Text
appointment.Start = Von
appointment.End = Bis
appointment.Location = "Raum 202 Kleinostheim"
appointment.RequiredAttendees.Add("geve#krag.de") // HERE ARE THE Attendees
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
i need every email adresses exept the first, becuase he sends the mails.
how can i do that?
thanks in advance for your help

Here's how you would actually split the string into a string array:
Dim emails As String() = emailString.Split({";"}, StringSplitOptions.RemoveEmptyEntries)
There are other versions of the overloaded "Split" method, but that particular one lets you to pass in a StringSplitOptions value, allowing you to rule out blank entries right away.
After you have the string array, you can loop through and omit the first one in a few different ways.
We could use a For loop and skip the first entry entirely:
' Regular For loop approach
Dim emails As String() = emailString.Split({";"}, StringSplitOptions.RemoveEmptyEntries)
Dim appointment As New EWS.Appointment(esb)
With appointment
.Subject = "Einladung zu einem Termin"
.Body = txtThema.Text
.Start = Von
.End = Bis
.Location = "Raum 202 Kleinostheim"
End With
' start at i = 1 to skip index 0
For i = 1 To emails.Length - 1
appointment.RequiredAttendees.Add(emails(i)) ' HERE ARE THE Attendees
Next
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
Or identify the sender's email and use a For Each with an If to omit it:
' For Each approach
' set up string array and appointment object like above
Dim sender As String = emails.FirstOrDefault()
' ignore all instances of the sender's address
For Each address In emails
If address.ToLower <> sender.ToLower Then
appointment.RequiredAttendees.Add(emails(i)) ' HERE ARE THE Attendees
End If
Next
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
I'd say play around with it though, and use the approach that best suits you.

You can use the split function to parse out each email and use Linq to remove blanks and Skip the first entry. I think this syntax will work in VB.
Dim emailString = "geve#krag.de;;;Tobias#nxs.de;Wissel#weg.de;Sand#nex.de;Claudia#bea.de;;"
Dim emaillist = (From email In emailString.Split(";").Skip(1) Where email.Length > 0 Select email).ToArray()

Related

Delete duplicate words in a string that has concatenated other variables previously

I have a form in MS Access that collects feedback from 4 users. After users have filled the form there's a variable called "sResultAll" that concatenates all feedback (from several textboxes) into it and passes its value to a Textbox called "txtRecommendation".
sResultAll = sResult1 & sResult2 & sResult3 & sResult4
txtRecommendation.Value = sResultAll
The problem I face is several users provide literally the same feedback so I need a way to eliminate duplicates within the variable sResultAll.
Thanks in advance for any contribution.
Friday evening 5:55, got nothing else to do. Here's a freebie:
As #braX suggested, a Dictionary Object is great at keeping track of unique strings because it can quickly search its current keys with the .Exists function. This lets you check if the string has already been entered, before adding it to the collection.
To improve this idea, I also suggest that you sanitize the strings before comparing them. Force them all to the same case and remove non-alphanumeric characters. This way, the strings will still match regardless of white-space, punctuation, or capitalization.
Sub Example()
Const sResult1 As String = "George"
Const sResult2 As String = "Fred"
Const sResult3 As String = "John"
Const sResult4 As String = "gEORGE "
Debug.Print Join(DistinctOf(sResult1, sResult2, sResult3, sResult4), ", ")
'Outputs: George, Fred, John
End Sub
Function DistinctOf(ParamArray Strings() As Variant) As Variant()
Dim AlphaNumericOnly As Object
Set AlphaNumericOnly = CreateObject("VBScript.RegExp")
With AlphaNumericOnly
.Global = True
.MultiLine = True
.Pattern = "[^A-Za-z0-9]+"
End With
Dim Distinct_Strings As Object
Set Distinct_Strings = CreateObject("Scripting.Dictionary")
Dim str As Variant
For Each str In Strings
Dim AO_str As String
AO_str = AlphaNumericOnly.Replace(LCase(str), "")
If Not Distinct_Strings.exists(AO_str) Then Distinct_Strings.Add AO_str, str
Next
DistinctOf = Distinct_Strings.Items
End Function

Copying a portion of a string using vba

I have to get username from MeetingItem.Recipient, I tried following to get it:
CStr(MeetingItem.Recipient.Address) and got this responce:
"/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr" I have to do loop through all
recipients and get usernames for example if i do loor with code above Ill get:
"/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=kisslingie0e"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=katzensteink"
"/O=POST/OU=Bern/cn=Recipients/cn=junkerb"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=tanzg6a7"
I need only last part of this strings, how can i do that?
note: kisslingie0e and tanzg6a7 this nicknames contains at the end unnecessary three characters that must also be avoided
Or is there another way to get usernames from MeetingItem.Recipient.Adress.
To get Email I did following:
For Each recip In recips
'Obtain the E-mail Address of a Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set pa = recip.PropertyAccessor
Dim email as String
email = CStr(pa.GetProperty(PR_SMTP_ADDRESS))
Debug.Print email
End For
Use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress to get the SMTP address.
Be prepared to handle nulls and errors.
To get the NT login name (domain account), read the PR_ACCOUNT MAPI property (DASL name http://schemas.microsoft.com/mapi/proptag/0x3A00001F) using Recipient.AddressEntry.PropertyAccessor.GetProperty.
You can also use Recipient.AddressEntry.GetExchangeUser().Alias
The easiest way to remove the leading text is to reverse the string and loop until you find a "/":
Dim email As String, username As String
Dim i As Integer
email = "/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=kisslingie0e"
'Reverse string
email = StrReverse(email)
'Loop through string until / is found
For i = 1 To Len(email) Step 1
If Mid(email, i, 1) = "/" Then
Exit For
Else
username = username & Mid(email, i, 1)
End If
Next i
'Reverse username
username = StrReverse(username)
If you need to remove the "cn=", do something like this:
username = Split(username, "=")(1)
If the usernames never contain any numbers, you could remove the trail like this:
For i = 1 To Len(username) Step 1
'Loop until a number occurs
If IsNumeric(Mid(username, i, 1)) Then
'Use string until the number
username = Mid(username, 1, i - 1)
Exit For
End If
Next i
Here's another suggestion that works IF the source is consistent in having "Recipients/cn=" just prior to the desired string, it is followed by optionally stripping the last characters if they are numeric in the third or second to last character.
'find the location of constant, set vEM
vLoc = InStr(email, "Recipients/cn=")
vEM = Mid(email, vLoc + 14, 50)
'Check if third to last or second to last character is numeric
vOffset = 0
If IsNumeric(Mid(vEM, Len(vEM) - 2, 1)) Then
vOffset = 3
ElseIf IsNumeric(Mid(vEM, Len(vEM) - 1, 1)) Then
vOffset = 2
Else
vOffset = 0
End If
vEM = Left(vEM, Len(vEM) - vOffset)

How to compare strings in Vb.Net from 2 different text boxes?

Im making a program where I want to be able to paste in my email list, then search through the list and compare them to a list of strings in another textbox, and if any of it matches, then the email address gets moved to a third textbox.
For example, if I wanted to filter all the gmails and hotmails, I would enter them in the box, paste the emails in the other box and click go.
But it doesn't seem to work properly, with only a few entries it seems to work fine, but if i paste more than a few emails it only seems to detect gmails(or whatever the first entry i have in the compare textbox).
I hope this makes sense I can't figure out why it wont work.
Here is my code
Dim compare As String
Dim comparear() As String
Dim list As String
Dim listar() As String
compare = txtcompare.Text
comparear = compare.Split(vbNewLine)
list = txtlist.Text
listar = list.Split(vbNewLine)
For i = 0 To comparear.Length - 1
For p = 0 To listar.Length - 1
If listar(p).Contains(comparear(i)) Then
txtresult.Text = txtresult.Text & listar(p)
Else
End If
Next
Next
Replace this line:
comparear = compare.Split(vbNewLine)
...
listar = list.Split(vbNewLine)
With this:
comparear = compare.Split(",")
...
listar = list.Split(",")
If you split the search criterias into commas(,)

Replacing string at certain index of Split

Using streamreader to read line by line of a text file. When I get to a certain line (i.e., 123|abc|99999||ded||789), I want to replace ONLY the first empty area with text.
So far, I've been toying with
If sLine.Split("|")(3) = "" Then
'This is where I'm stuck, I want to replace that index with mmm
End If
I want the output to look like this: 123|abc|99999|mmm|ded||789
Considering you already have code determining if the "mmm" string needs to be added or not, you could use the following:
Dim index As Integer = sLine.IndexOf("||")
sLine = sLine.Insert(index + 1, "mmm")
You could split the string, modify the array and rejoin it to recreate the string:
Dim sLine = "123|abc|99999||ded||789"
Dim parts = sLine.Split("|")
If parts(3) = "" Then
parts(3) = "mmm"
sLine = String.Join("|", parts)
End If
I gather that if you find one or more empty elements, you want to replace the first empty element with data and leave the rest blank. You can accomplish this by splitting on the pipe to get an array of strings, iterate through the array and replace the first empty element you come across and exit the loop, and then rejoin your array.
Sub Main()
Dim data As String = "123||abc|99999||ded||789"
Dim parts = data.Split("|")
For index = 0 To parts.Length - 1
If String.IsNullOrEmpty(parts(index)) Then
parts(index) = "mmm"
Exit For
End If
Next
data = String.Join("|", parts)
Console.WriteLine(data)
End Sub
Results:
123|mmm|abc|99999||ded||789

VB.NET how to extract recipients for an Outlook appointment?

NET developers.
I am trying to replicate what I've written in Outlook VBA in VB.NET. I've written a function in VBA to extract recipients and save them into a string and this is done in a function. The below code is my attemp in VB.NET and it is not really working at the moment(SplitTarget array has unique ID and I'm testing this on the first element at the moment).
Could you tell me where I've gone wrong?
Dim Recipients As String
Dim Obj As Object
Dim types() As String
types = Split("MailItem,AppointmentItem,JournalItem,MeetingItem,TaskItem", ",")
Select Case True
' these items have a Recipients collection
Case UBound(Filter(types, TypeName(objNamespace.GetItemFromID(GlobalVariables.splitTarget(0))))) > -1
Obj = objNamespace.GetItemFromID(GlobalVariables.splitTarget(0))
GlobalVariables.recips = Obj.Recipients
Case TypeName(objNamespace.GetItemFromID(GlobalVariables.splitTarget(0))) = "Recipients"
GlobalVariables.recips = objNamespace.GetItemFromID(GlobalVariables.splitTarget(0))
End Select
For k = 1 To GlobalVariables.recips.Count
If GlobalVariables.recips(k).ToString <> "Caseflow System" Then
If Recipients = "" Then
Recipients = GlobalVariables.recips(k).ToString
Else
Recipients = Recipients & ";" & GlobalVariables.recips(k).ToString
End If
End If
Next
MsgBox("Recipients are: " + Recipients)
Why are you calling Recipient.ToString() (which comes from .Net, not OOM) instead of using Recipient.Name/Address/etc. properties?