Searching a text file for a string and displaying it

Justin.B

New Member
Joined
Jul 1, 2008
Messages
30
Okay so I have a new project. I created a help file VIA a text file ( I know I could use .chm or .hlp but I would rather use a .txt file ) now once a user clicks a button I want it to open that text file and search for a string of text and locate and show that section of text. My code is pretty messy and nasty so far but here. Im using this withing a userform. Please help.

Code:
Private Sub ClickLabel1()
Dim filepath As String
Dim srtxt As String
filepath = "C:\Program Files\Excel\wtithelp.txt"
srtxt = "Topic 1"
Function fnFindText(filepath, srtxt)
strFileRes = fnFileExistence(filepath)
If strFileRes = "File Exists" Then
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filepath, ForReading, True)
Do While f.AtEndOfStream <> True
A = f.readline
'MsgBox a
If InStr(A, srtxt) <> 0 Then
boolStFnd = "True"
fnFindText = "Text found"
Exit Do
End If
Loop
If boolStFnd <> "True" Then
fnFindText = "Text not found"
End If
f.Close
Else
fnFindText = "File does not exists"
End If
End Function

I can't get this to work for the life of me. Someone please help. Thank you very much.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This will get you a little closer. You need to declare your functions a little more carefully. I could not really figure out what you were doing, so some of it is commented out. I used a command button instead of a label.

Code:
Private Sub CommandButton1_Click()
Dim filepath As String
Dim srtxt As String
filepath = "C:\Program Files\Excel\wtithelp.txt"
srtxt = "Topic 1"
Call fnFindText(filepath, srtxt)
Unload Me
End Sub
Function fnFindText(filepath, srtxt)
'strFileRes = fnFileExistence(filepath)
'If strFileRes = "File Exists" Then
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filepath, ForReading, True)
 Do While f.AtEndOfStream <> True
  A = f.readline
  If InStr(A, srtxt) <> 0 Then
   boolStFnd = True
   fnFindText = "Text found"
  Exit Do
  End If
 Loop
If boolStFnd <> True Then
 fnFindText = "Text not found"
Else
 fnFindText = "The text " & srtxt & " was found"
End If
 f.Close
 MsgBox fnFindText
End Function
 
Upvote 0
Basically what I am trying to do is that when the user clicks this button located on the userform it will open a text file (help file) and go to find specific text and the user will be able to view the file from there.

Basically the #section function of HTML except this is within a text document.
 
Upvote 0
This will get you a little closer. You need to declare your functions a little more carefully. I could not really figure out what you were doing, so some of it is commented out. I used a command button instead of a label.

Code:
Private Sub CommandButton1_Click()
Dim filepath As String
Dim srtxt As String
filepath = "C:\Program Files\Excel\wtithelp.txt"
srtxt = "Topic 1"
Call fnFindText(filepath, srtxt)
Unload Me
End Sub
Function fnFindText(filepath, srtxt)
'strFileRes = fnFileExistence(filepath)
'If strFileRes = "File Exists" Then
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filepath, ForReading, True)
 Do While f.AtEndOfStream <> True
  A = f.readline
  If InStr(A, srtxt) <> 0 Then
   boolStFnd = True
   fnFindText = "Text found"
  Exit Do
  End If
 Loop
If boolStFnd <> True Then
 fnFindText = "Text not found"
Else
 fnFindText = "The text " & srtxt & " was found"
End If
 f.Close
 MsgBox fnFindText
End Function

Okay now I see what you did but lets say I want to open the text file in a notepad or any other word processing program and find the string of text that it did a search for and have that area be pulled up on screen how would I go about doing so? My guess is that it would need to be here
Code:
Else
 fnFindText = "The text " & srtxt & " was found"
workbooks.opentext FileName:="C:\Program Files\Excel\wtithelp.txt"
End If

That code is most likely wrong but if someone could help it would be greatly appreciated.
 
Upvote 0
If I were you I would put your help on a separate worksheet tab instead of a separate text file. That way you can easily display it in text box or any number of ways very easily. You can open your text file with notepad, but you then have no control over that app. At least that has been my experience.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top