Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

how can i copy

Forum 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

ResolvedVersion 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 courses

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
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Access tip:

Zoom feature for queries

If youare working on on query design and creating a calculated field, it can be hard to view the whole statement. You can hold the shift key down and press F2, to show the zoomed up version of the query field

View all Access hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.