Searching text in a .txt file using visual basic - vb.net

I am making a project that searches a document based on some words provided by the user in a text box to search in the text file.
I am using visual studio 2013 on a basic windows form application, and want to open file component based on the word in the textbox.
the text must open from the required point.

If i've understood your question, you want to find a keyword inside a txt file based on the user input in order to do something, well. This is a little snippet to give you some example and a good starting point.
Snippet
Dim reader As StreamReader
Dim txtInput as String = "YOUR_TXT_FILE_PATH"
'The reader opens the txt input specified with the file path
reader = My.Computer.FileSystem.OpenTextFileReader(txtInput)
'Create a string from the txt file
Dim txtString as String = reader.ReadLine.TrimStart
Dim wordFound as String
'Use Regex inside a Try/Catch statement in order to catch any possible exception.
'If Regex.Match doesn't find your TextBox1.Text inside the string it will throw an exception
Try
'Use Regex to find your word or your expression inside the string created before
For Each data As Match In Regex.Matches(txtString, TextBox1.Text)
wordFound = txtString.Substring(data.index, TextBox1.Text.Lenght())
'Do your stuff with your word found in the txt...
Next
Catch ex As Exception
Err.Clear()
Finally
'Close the reader before the Try/Catch statement
reader.Close()
End Try
I suggest you to first read the documentation about Regex.Match and if you have any doubt feel free to ask.
Welcome to StackOverflow!

Related

Visual Studio 2017 Visual Basic - How to pass variable from one event to another

I'm learning VS VB, so please forgive the ignorance.
I have built a form that accepts a file search pattern, searches for matching files and then displays the file names (up to 10) in text boxes.
If you double click on the file name (up to 10) text box, I want it to call a routine to open Powerpoint and display the first slide.
I've searched around and can't find any information thats helpful to me for 2017.
At this point, my code looks like below (just shows that I have the full file name).
Public Sub DisplaySong(ByVal SongFileName)
Dim SongToDisplay As String = ""
Dim i As Integer
' get the full song path/name
Try
Dim strFiles As String() = Directory.GetFiles(strSearchPath.ToString, SongFileName.ToString, IO.SearchOption.AllDirectories)
i = 0
' should never be more than 1 song
For Each SongFileName In strFiles
SongToDisplay = strFiles(i)
i = i + 1
Next
Catch ex As Exception
MessageBox.Show("Error processing Display Song: Finding Song!! ")
End Try
MessageBox.Show("Song Selected: " + SongToDisplay.ToString)
End Sub
The examples I've talk about adding the COMs, but I don't see "Microsoft Graph Object Library" in the 2017 version. I selected MS PowerPoint and the MS Graph 14.0, thinking (hoping) that's what I needed.
When I add
Dim oApp as PowerPoint.Application
into the subroutine, I get the error message that it's not defined. I know how to do this in Access VB 2010, but I don't know how to do this in VS VB 2017.
Any help is appreciated.
Dummy me. Adding
Imports Microsoft.Office.Interop
Fixed the problem.

VBA - Reading PDF as String - Cannot sometimes but can other times - 'Run time error 62' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
You can open PDFs in text editors to see the structure of how the PDF is written.
Using VBA I have opened a PDF as a text file and go to extract the text and save it as a string variable in VBA. I want to look through this text to find a specific element; a polyline (called sTreamTrain) and get the vertices of the polyline by using the InStr function.
When i add more vertices to the polyline I cannot seem to extract the text string of the pdf. I get the error 'Run time error 62' which I do not understand what it means or what about the PDF has changed to now have this error.
Attached (via the link) is a PDF that I can read (Document 15) and a PDF I cannot read (Document 16). I have checked in excel so see that the vertices are present in both files. Also there is a copy of my VBA script as a notepad document and also my excel file (but it is difficult to find in my excel file - the script is "Module 6" function called "CoordExtractor_TestBuild01()")
Link:
https://drive.google.com/open?id=1zOhwnFWZZfy9bTAxKiQFSl7qiQLlYIJV
Code snippet of the text extraction process below to reproduce the problem (given an applicable pdf is used):
Sub CoordExtractor_TestBuild01()
'Opening the PDF and getting the coordinates
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
'File Path of Text File
FilePath = "C:\Users\KAllan\Documents\WorkingInformation\sTreamTrain\Document16 - Original.pdf"
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
'Store file content inside a variable
Dim Temp As Long
Temp = LOF(TextFile)
FileContent = Input(LOF(TextFile), TextFile)
'Clost Text File
Close TextFile
End Sub
I would like someone to let me know what runtime error 62 is in this context and propose any workflows to get around it in future. Also, I would like to know whether there certain characters you cannot store as strings? - Perhaps these are included when I increase the number of vertices past a certain number.
Also I would prefer to keep the scrips quite simple and not use external libraries because I want to share the script when it is done so others can use it thus its simpler if it works without extra dependencies etc, however, any and all advice welcome since this is only the first half of this project.
Thank you very much.
According to the MSDN documentation, this error is caused by the file containing
...blank spaces or extra returns at the end of the file or the syntax
is not correct.
Since your code works sometimes on documents with very similar names and content to documents where it doesn't work, we can rule out syntax errors in this case.
You can clean up the file contents before processing it any further by replacing the code at the top of your macro with the one below. With this I can read and extract information from your Document16.pdf:
Sub CoordExtractor_TestBuild01()
'Purpose to link together the extracting real PDF information and outputting the results onto a spreadsheet
'########################################################################################
'Opening the PDF and getting the coordinates
Dim n As Long
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
'File Path of Text File
FilePath = "C:\TEST\Document16.pdf" ' change path
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
Dim strTextLine As String
Dim vItem As Variant
Line Input #1, strTextLine
vItem = Split(strTextLine, Chr(10))
' clean file of garbage information line by line
For n = LBound(vItem) To UBound(vItem)
' insert appropriate conditions here - in this case if the string "<<" is present
If InStr(1, vItem(n), "<<") > 0 Then
If FileContent = vbNullString Then
FileContent = vItem(n)
Else
FileContent = FileContent & Chr(10) & vItem(n)
End If
End If
Next n
'Clost Text File
Close TextFile
' insert the rest of the code here

How can I close an opened file after finishing using at the end of code execution in Vb.net

I am reading a word document inside a folder with vb.net, then I fill some data fields inside the document, after that I convert the word document into a pdf file and I save it in the same folder of my word document.
The problems becomes when I go to the folder where my word template is located, if I try to delete the word file, it shows me an error that say: "The document is been using by XML process", the only way to delete my file is killing the process in the task manager, which is means after finishing executing the code, the word file is still opened.
I try to use at the end of the code: Doc.Close() but the compiler says that the sentences are ambiguous.
This is the code I am using:
Dim missing As Object = Type.Missing
Dim app As New Word.Application()
Dim doc As Word.Document=app.Documents.Open(Server.MapPath("~\\App_Tmp\\WordTemplate1.docx"), missing, True)
Dim fields As Word.FormFields = doc.FormFields
fields("tLegalName").Result = lawFirmObj.DisplayName
doc.Activate()
doc.SaveAs2(Server.MapPath("~\App_Tmp\Filled.pdf"), WdSaveFormat.wdFormatPDF)

parsing a large csv file using vb.net without newline ending

I was given a file that was created with a java program but does not have LF or endofline ending so I am working with a gigantic string. I tried splitting and then using the TextFieldParser but it seems the file is just too big to deal with. The contents are vital to I need to get this data somehow and then clean it up. Here is what I have tried:
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\Desktop\META3.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
I think the best way is to take substrings of the text and I wanted to take all values after the 7 occurrences of a comma which is what the file should have per line. Not sure how to do this and it seems like regex maybe the only option. Any ideas appreciated.
line = freader.Readline()
Dim ms As Match = Regex.Match(line, "(\w+),(\w+),(\w+),(\w+),(\w+),(\w+),")
line = ms.Value
will this work; does not give expected results.
If you can be guaranteed that the number of columns is always consistent why not add a counter that reads each column and then goes onto the next set. You can then create a new spreadsheet or file with the correct format. If you do an search on here there is a package for .net which allows you to build valid .xls and .xlsx files on the fly. The package is called "simple ooxml". i have used this to create all sorts of spreadsheets where I work. I built a command line app that passes and xml file with parameters and builds this as a fully fledged spreadsheet. Hope the above helps. Any questions let me know.

System.IO.StreamReader returns corrupted output

The below given code is returning corrupted data to the variable 'mystr'
like
PK ! ��fѲ �  [Content_Types].xml �(� �UMk�#���^��N%�9�ɱ
It was reading the word files correctly. Suddenly it started happening without any change in code or software versions or source files!! Any word file I try to run through the below code gives the same corrupted output. I'm able to open the file in MsWord without any issue.
Dim myStreamReader As System.IO.StreamReader
Dim myStr As String
myStreamReader = System.IO.File.OpenText("c:\test.docx")
myStr = myStreamReader.ReadToEnd()
myStreamReader.Close()
Any suggestions why it is happening ?
You can't read a Word document using the StreamReader class as the file itself isn't plain text (it contains the data you find 'corrupted').
This link will help you reading text from a Word document.