Microsoft Office Tutorials and References
In Depth Information
Using Text Files to Store External Data
Next LRow
Close #1
Next iWks
MsgBox “The text files can be found in “ & Left(sPath, Len(sPath) - 1)
End Sub
If you would like to see a text file’s contents in a Message Box, you can use the following code.
Sub GetTextMessage()
Dim sTxt As String, sText As String, sPath As String
sPath = “C:\YourFilePath\YourFileName.txt”
If Dir(sPath) = “” Then
MsgBox “File was not found.”
Exit Sub
End If
Open sPath For Input As #1
Do Until EOF(1)
Line Input #1, sTxt
sText = sText & sTxt & vbLf
sText = Left(sText, Len(sText) - 1)
MsgBox sText
End Sub
Suppose you want to save the contents of cell A1 on Sheet1 as a text file. The following example
shows how that can be done:
Sub SaveCellValue()
Open “C:\YourFilePath\YourFileName.txt” For Append As #1
Print #1, Sheets(“Sheet1”).Range(“A1”).Value
Close #1
End Sub
Finally, this macro demonstrates how to delete a text file if it exists, and replace it with a new text
file of the same name. If the text file does not exist, the macro will create a new text file.
Sub DeleteAndCreate()
Dim strFile As String, intFactor As Integer
On Error Resume Next
strFile = “C:\YourFilePath\YourFileName.txt”
Kill strFile
intFactor = FreeFile
Open strFile For Output Access Write As #intFactor
Close #intFactor
End Sub
Search JabSto ::

Custom Search