file I/O

tranchemontaigne

Registered User.
Local time
Today, 14:09
Joined
Aug 12, 2008
Messages
203
OBJECTIVE:
I am trying to figure out how to create text files using VBA.

PROBLEM:
Unfortunately none of my books address FileSystemObjects. Code samples I have seen on the net don't work for me and I wonder if I need a special library reference to make this work.

FUNCTION OBJECTIVE:
Function should be able to receive a string variable and create a new text file that contains nothing other than the string variable's value

Here's a snippet of my broken code

Public Function fnTest(strPayload as string)
'//////////////////////////////////////////////////////////////////
'// Requirements:
'// Microsoft Visual Basic for Applications
'// Microsoft Access 9.0 Object Library
'// Microsoft Scripting Runtime (scrrun.dll)
'//
'//////////////////////////////////////////////////////////////////
Dim fso As New FileSystemObject
Dim a As FileSystemObject

strPayload = "This is a test

Set fso = CreateObject("Scripting.FileSystemObject")

Set a = fso.CreateTextFile("C:\My Documents\TestFile.txt", True)
a.WriteLine (strPayload )
a.Close

MsgBox "done"

End Function
________
Cloudy Trichomes
 
Last edited:
There is no need to use FSO to create files, there is an access native function for that.

Have a search in the help on "open statement" and read the use and look at the sample given.

Come back here for specific questions :)
 
Thanks!

this code now works

Public Function fnTest2()

Dim Pathname As String

Pathname = "C:\temp\test.txt"

'create document
Open Pathname For Output Access Write As #1

'write data to file
Write #1, "This is test content"

'close connection to file
Close #1

'notify that fnTest2 completed
MsgBox "done"

End Function
________
Honda Cbr600F
 
Last edited:

Users who are viewing this thread

Back
Top Bottom