98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Access VBA Training and help » How can I copy text out of a text file
How can I copy text out of a text file
Resolved · Medium Priority · Version 2007
Adrian has attended:
Access VBA course
How can I copy text out of a text file
I need to extract text from a text file that may contain up to 25k records. Can I copy the desired text and paste it inot a new text file using VBA??
RE: How can I copy text out of a text file
Hi Adrian
Thanks for getting in touch. Apologies for the delay in answering while we researched your query.
You will need to use the FileStreamObject. To work with this in the VBE go to Tools > Options > References. Check the box next to Microsoft Scripting Runtime and click OK.
Then you can use the following code snippets as examples of how you can read and write text files.
Sub CreateFile()
Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso.CreateTextFile("C:\MyFile.txt", True)
ts.WriteLine ("First line of text")
ts.WriteLine ("second line of text")
ts.Close
End Sub
Sub ReadFile()
Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso.OpenTextFile("C:\MyFile.txt", ForReading)
Dim ThisLine As String
Dim i As Integer
i = 0
Do Until ts.AtEndOfStream
ThisLine = ts.ReadLine
i = i + 1
Debug.Print "Line " & i, ThisLine
Loop
ts.Close
End Sub
I hope this helps.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Access tip:Create own ribbon tab - Access 2010a. In Access click on the File tab |