Copying a portion of a string using vba - 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)

Related

VB.net Read Specific Lines From a Text File That Start With and Stop Reading When Start With

I'm looking to read lines from a text file that start with certain characters and stop when the line starts with other characters. So in my example I would like to start reading at line AB and stop at line EF however not all lines will contain the CD line. There will always be a AB line and EF line, however the number of lines in between is unknown.
Here is an example of the lines in a text file I would be reading. You can see that this will create two rows in the DataGridView however the first row is missing the CD line and should be blank.
AB-id1
EF-address1
AB-id2
CD-name1
EF-address2
Here is the code I have so far:
Dim lines() As String = File.ReadAllLines(textfile)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("AB") Then
Dim nextLines As String() = lines.Skip(i + 1).ToArray
Dim info As String = nextLines.FirstOrDefault(Function(Line) Line.StartsWith("CD"))
Dim name As String = "Yes"
Dim info2 As String = nextLines.FirstOrDefault(Function(Line) Line.StartsWith("EF"))
Dim address As String = "Yes"
End If
DataGridView.Rows.Add(name,address)
Next
Now the output I currently get is:
|Yes|Yes|
|Yes|Yes|
And I should be getting:
||Yes|
|Yes|Yes|
It looks like it's reading too far down the text file and I need it to stop reading at EF. I've tried Do while and Do Until with no success. Any suggestions?
You could use the Array.FindIndex function to get the index of the next line starting with your prefix. This way you don't have to skip lines and create a new array each time.
Try this out instead:
Dim lines() As String = File.ReadAllLines(textFile)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("AB") Then
Dim addressIndex As Integer = Array.FindIndex(lines, i + 1, Function(Line) Line.StartsWith("EF"))
Dim address As String = If(addressIndex <> -1, lines(addressIndex).Substring(3), "") ' Get everything past the "-"
Dim name As String = ""
If addressIndex <> -1 Then
Dim nameIndex As Integer = Array.FindIndex(lines, i + 1, addressIndex - i, Function(line) line.StartsWith("CD"))
If nameIndex <> -1 Then
name = lines(nameIndex).Substring(3) ' Get everything past the "-"
End If
End If
DataGridView.Rows.Add(name, address)
End If
Next

VB.NET - Get second hyperlink from string and set as LinkLabel text property

I have a tool that takes in an email and sorts the data into the appropriate fields. In the email, there are normally two hyperlinks that launch the ticket editor in IBM notes. One is a local link and one is a server link. The following code is able to get the local link as it is the first hyperlink that contains 'notes://' but I need to get the one for the server as well, which also starts with 'notes://".
For i = 0 To lines.Count + 1
If lines(i).StartsWith("notes://") Then
StartLine = i
Exit For
End If
Next
link_OpenRequestUsingNotes.Text = lines(StartLine)
I thought of writing some code to check the line about it to see if it matches a specific string, maybe with Regex?
For the local link it needs to look for:
"using a Notes client (local replica)"
For the server link it needs to look for:
"using a Notes client (Server):"
The hyperlink is below this text so it would need to look for those and then get the text on the next line.
These links are then set as the text for two link labels that the users can press and go to that link.
If someone could please help me find a way to get the server link, it would be greatly appreciated.
You can use this code:
link_OpenRequestUsingNotes_Local.Text = "" 'fields for local link
link_OpenRequestUsingNotes_Server.Text = "" 'field for server link
For i = 0 To lines.Count - 1
If lines(i).StartsWith("using a Notes client (local replica)") Then
link_OpenRequestUsingNotes_Local.Text = lines(i + 1)
ElseIf lines(i).StartsWith("using a Notes client (Server)")
link_OpenRequestUsingNotes_Server.Text = lines(i + 1)
End If
If link_OpenRequestUsingNotes_Local.Text <> "" AndAlso link_OpenRequestUsingNotes_Server.Text <> "" Then
Exit For
End If
Next i
You could use code like this to find the first two links:
Dim notes = Function(x) x.StartsWith("notes://")
Dim index As Integer = lines.FindIndex(notes)
If index <> -1 AndAlso index < (lines.Count - 1) Then
Dim firstLink As String = lines(index + 1)
' ... do something with "firstLink"...
Debug.Print(firstLink)
index = lines.FindIndex(index + 2, notes)
If index <> -1 AndAlso index < (lines.Count - 1) Then
Dim secondLink As String = lines(index + 1)
' ... do something with "secondLink"...
Debug.Print(secondLink)
End If
End If
I managed to fix it myself
Just add 1 to i (StartLine = i + 1) to go to the line below!
'Notes Server Link
For i = 0 To lines.Count - 1
If lines(i).StartsWith("Click here to open the request using a Notes client (Server):") Then
StartLine = i + 1
Exit For
End If
Next
link_OpenServerLink.Text = lines(StartLine)

VBA in ACCESS. Trouble with ComboBox

I have some ComboBoxes on my FORM. One of them have items as a result of SQL request from field PG (cbPG.RowSource = "SELECT DISTINCT W_report.PG FROM W_report WHERE ......) The size of the field is byte.
After reqest
User can select one of the variant or can list several comma-separated (2,4,5,7,11,13).
Correct value
The resulting ComboBox.value is used in a procedure similar to selecting pages for printing. Everything works correctly until changes are made to the event handler of cbPG. Then the values are automatically rounded (if one comma)
wrong value
or an error "The entered value is not appropriate for this field" occurs (if a few commas) and I have to copy cbPG from the backup because I can't find a property that changes format of cbPG.value to byte.
Here is part of program that use my ComboBox
Public Function MnogoListov(str As String) As String
Dim i, j As Integer
Dim res As String
Dim listArr() As String
res = ""
ReDim listArr(Len(str)) As String
For i = 1 To Len(str)
If Mid(str, i, 1) <> "," And Mid(str, i, 1) <> "." Then
listArr(j) = listArr(j) & Mid(str, i, 1)
Else
j = j + 1
End If
Next
For i = 0 To j
If i = 0 Then
res = listArr(i)
Else
res = res & " OR W_report.PG = " & listArr(i) End If
Next
MnogoListov = res
End Function
You can't do that. A combobox is for selecting one value from several.
So, either use a multi-select listbox or a simple textbox where you - similar to selecting pages for printing - parse the inputted values to obtain the sequence (list) of items (pages).

How can i split a String and use them in a Loop in VB?

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()

How can I get the nickname and message from raw IRC data in vb.net

Well basically I've got a vb.net script connecting to IRC, and I'm working on making it a basic chat system, but I've run into a problem.
Say I receive this:
:nickname!name#tw-32151D9B.hsd1.vt.comcast.net PRIVMSG #channel :message
I want to grab specific information to output to the user.
I want to grab nickname and message
How can I go about doing this?
I thought about using regex, but I can't figure out how to make regex grab message since there's nothing after it.
I love IRC. The following code will do what you want assuming your raw data is in the variable strData.
Dim strNickName As String = String.Empty
Dim strMessage As String = String.Empty
Dim intToMessage As Integer = 0
Dim intParse As Integer = 0
intParse = InStr(strData, "!")
strNickName = Mid(strData, 2, (intStart - 2))
intToMessage = InStr(strData, "PRIVMSG #")
intParse = InStr(Mid(strData, intToMessage, (Len(strData) - intToMessage)), ":")
strMessage = Mid(strData, (intToMessage + intStart), (Len(strData) - (intToMessage + intStart - 1)))
You can use RegEx to get everything between the first : and !
(?<=:).*?(?=!)
and then look for everything between the last #channel : and the end of the line
(?<=#channel :).*?(?=$)
This is simple but should take into account that someone may use a semi-colon (:) in the message.