|
|
|
|
|
|
Word
|
Read Full Content Of Text File (Fast)
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2000, 2002
|
Submitted by:
|
MOS MASTER
|
Description:
|
A fast way to read the entire contents of a txt file. This function will return it as a string.
|
Discussion:
|
You need a way to display the content of a txt file in a userform or Message box. Both are in the attachment as examples to show how to call the function.
|
Code:
|
instructions for use
|
Option Explicit
Public Function LoadTextFile(sFile As String) As String
Dim iFile As Integer
On Local Error Resume Next
iFile = FreeFile
Open sFile For Input As #iFile
LoadTextFile = Input$(LOF(iFile), iFile)
Close #iFile
End Function
Sub Test()
Dim sPath As String
sPath = ThisDocument.Path & "\dummy.txt"
MsgBox LoadTextFile(sPath)
End Sub
|
How to use:
|
- Open your document.
- Press Alt + F11 to open VBE.
- Insert-Module. (Insert -> module)
- Paste the code in the right (code) pane. (Ctrl + V)
- Close VBE (Alt + Q or press the X in the top right hand corner).
- Save the file.
|
Test the code:
|
- On open a userform will pop-up with the contents of the text file (example)
- To call the manual sub:
- press Alt + F8 to open the macro dialog box.
- Select Test
- Click Run.
|
Sample File:
|
Input Txt file.zip 11.14KB
|
Approved by mdmackillop
|
This entry has been viewed 194 times.
|
|