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

resolvedResolved · 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 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:

Create own ribbon tab - Access 2010

a. In Access click on the File tab
b. Select Options from left hand side
c. Choose the Customize Ribbon section
d. Click the New Tab button (below the list of tabs on the right hand side of the dialog box)
e. Select the New Tab (Custom) and enter a name, by clicking the Rename button (below the list of tabs on the right hand side of the dialog box)
f. Select the New Group (Custom) and enter a name, by clicking the Rename button (below the list of tabs on the right hand side of the dialog box)
g. Add commands to your tab and group by locating them on the list on the right hand side (remembering that you can change the list using the drop down box at the top of the list of commands) and clicking the Add button between the two panes to add them to your tab and group
h. You can rearrange the commands in your group, the groups on any tab or the tabs, using the up and down arrow buttons beside the list of tabs.
i. Click OK to apply your changes

View all Access hints and tips


Server loaded in 0.1 secs.