First of all we need to define the variables.
Dim myFile, text, textline as String
Variable "myFile" is used to store the file path of the text file. Each and every text lines will be stored temporarily to the variable call "textline" while program run throgh the Do loop until it reaches to the end of the file. All the text lines of the text file will be appended to variable "text".
There are two ways to assign the text file to "myFile" variable depending on your requirement.
Either you can use below line of code to assign the file path directly. This method can be used if you always use same folder path and file.
myFile = "D:\Fiverr\amjedamjed\Project 2\all - Copy (11).txt"
Or you can use below line of code to assign the file name to variable. Here user get opportunity to select the file through browsing window.
myFile = Application.GetOpenFilename()
So you should use one of the above two lines in your program according to end user requirement. However rest of the program will not affected by what you chooses from above two ways.
Below line will open the file for reading
Open myFile For Input As #1
Below do loop will run until it reaches to the end of the text file. Inside the do loop, program read the text file line by line and assign that single line to variable "textline". Whole text of text file is appended to variable call "text" by the next line.
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Below line of code will close the text file which was opened for reading.
Close #1
Now you have whole content of text file in variable "text". Now you can do any string manipulation to that text from here.
However I will just show the content in a message box.
MsgBox text
Here below is the full code for you to easily copy paste for your project
Dim myFile, text, textline as String myFile = "D:\Fiverr\amjedamjed\Project 2\all - Copy (11).txt" or myFile = Application.GetOpenFilename() Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline text = text & textline Loop Close #1 MsgBox text |