How do I save a text file? (1 Viewer)

David Anderson

Registered User.
Local time
Today, 03:18
Joined
Nov 18, 2007
Messages
84
I have written some module code in one of my Access 2003 apps that creates a text string of XML data. This string contains vbCrLf characters for each line end.

I now want to save this string as an XML text file on the hard disk of my PC but don't know how to do this.

Any assistance would be much appreciated.

David
 

MarkK

bit cruncher
Local time
Yesterday, 19:18
Joined
Mar 17, 2004
Messages
8,186
Here are two chunks of code I haven't tested ...
Code:
Sub CreateNewTextFile(filename As String, bodytext As String)
   Dim fso As New Scripting.FileSystemObject
   Dim ts As Scripting.TextStream
   
   Set ts = fso.OpenTextFile(filename, ForWriting, True)
   ts.Write bodytext
   ts.Close

End Sub


Sub CreateNewTextFile(filename As String, bodytext As String)
   Dim fso As Object
   Dim ts As Object
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set ts = fso.OpenTextFile(filename, 2, True)
   
   Dim varBody As Variant
   Dim var As Variant
   
   varBody = Split(bodytext, vbCrLf)
   For Each var In varBody
      ts.WriteLine var
   Next
   ts.Close

End Sub
The first is early bound and requires a reference to the Microsoft Scripting Runtime object model. It also might fail since the newline character in the TextStream (ts) may not be the same as vbCrLf.
The second is late bound, and splits your body text at the vbCrLf characters and explicitly writes each line to the TextStream.
The first is simpler, but may fail.
Cheers,
 

David Anderson

Registered User.
Local time
Today, 03:18
Joined
Nov 18, 2007
Messages
84
I've just tried your first solution and it appears to work perfectly. Thanks very much for your rapid assistance.

David
 

MarkK

bit cruncher
Local time
Yesterday, 19:18
Joined
Mar 17, 2004
Messages
8,186
You bet. Thanks for posting back 'cause I didn't really expect that first one to work.
Cheers,
 

Users who are viewing this thread

Top Bottom