how to get a word between 2 Certain words in textbox

i have text file contain info like this
1|word1 web
2|word2 web
3|word2 web
how to get the word between the (|).... and (web) in this text
Who is Participating?
sgayatriConnect With a Mentor Commented:
Add Project >> References >> Microsoft scripting runtime  to yur project

The followingcode will read a txt file called abc.log and print the words as required by you.
abc.log file should contain your txt as mentioned in your question

Dim fso As New FileSystemObject
    Dim fil1 As File, ts As TextStream
    If fso.FileExists("c:\windows\desktop\abc.log") Then
        Set fil1 = fso.GetFile("c:\windows\desktop\abc.log")
        Set ts = fil1.OpenAsTextStream(ForReading)
        While ts.AtEndOfStream = False
          str1 = ts.ReadLine
          MsgBox Trim(Split(Mid(str1, InStr(1, str1, "|") + 1), " ")(0))
       MsgBox "File abc.log does not exist!"
   End If
an option is
dim firstsplit
dim secondsplit
dim midword as string

firstsplit = split(text1.text, "|")
secondsplit = split(firstsplit, " ")
midword = secondsplit(0)
midword = either word1 or word2 in this case.

Cheers :)
MsgBox Mid("1|word1 web", 3, 5)
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Carl TawnSystems and Integration DeveloperCommented:
Or this:

    sVal = "1|word1 web"
    iStart = InStr(sVal, "|")
    sWord = Mid(sVal, iStart + 1, InStrRev(sVal, " web") - iStart)

Same principle as rickman35's solution, but a bit more flexible.

Hope this helps.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Private Sub Command1_Click()
    Dim word As String
    word = getWord("1|word1 web")
    If word <> "" Then
        MsgBox word
    End If
End Sub

Private Function getWord(ByVal strInput As String) As String
    Dim pipePos As Long
    Dim spacePos As Long
    pipePos = InStr(strInput, "|")
    If pipePos > 0 Then
        spacePos = InStr(pipePos, strInput, " ")
        If spacePos > 0 Then
            getWord = Mid(strInput, pipePos + 1, spacePos - pipePos - 1)
            MsgBox strInput, vbCritical, "Invalid Input"
        End If
        MsgBox strInput, vbCritical, "Invalid Input"
    End If
End Function
Try this:

strW = "1|word1 web"

Trim(Split(Mid(strW, Instr(1, strW, "|") + 1), " ")(0))
Try this

Plase a RichTextBox on your form. (if it is not part of component list add it - Ctrl+T)
I use this to quickly open a text file, you can also use open or the file object)

Private Sub Command1_Click()

  Dim str As String
  Dim a As Variant
  Dim i As Integer
  Dim sTmp As String
  Dim sFirst As String
  Dim sSecond As String

  RichTextBox1.LoadFile "g:\MyFile.txt"

  str = RichTextBox1.Text
  sFirst = "|"
  sSecond = "web"
  a = Split(str, vbCrLf)
  For i = 0 To UBound(a)
    sTmp = sTmp & Mid(a(i), _
            InStr(1, a(i), sFirst) + Len(sFirst), _
            InStrRev(a(i), sSecond) - Len(sSecond)) & vbCrLf
  Next i
  set a = nothing  
  MsgBox sTmp
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.