ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA - saving strings to a text file (https://www.excelbanter.com/excel-programming/383683-vba-saving-strings-text-file.html)

JohnJ

VBA - saving strings to a text file
 
Is there any Excel VBA code to save strings to a text file?

Andy Smith

VBA - saving strings to a text file
 
Look for the Open statement, whose See Also link should lead you to Close,
Print, Write, and even Input and Read.

--

--Andy Smith



"JohnJ" wrote:

Is there any Excel VBA code to save strings to a text file?


Chip Pearson

VBA - saving strings to a text file
 
Try code like the following to get started:

Dim FName As String
Dim FNum As Integer
Dim S As String

FName = "C:\Test\TestFile.txt"
FNum = FreeFile
S = "Write This"
Open FName For Output Access Write As #FNum
Print #FNum, S
Close #FNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"JohnJ" wrote in message
...
Is there any Excel VBA code to save strings to a text file?




Chip Pearson

VBA - saving strings to a text file
 
I should have noted that if you open the file for Output, the existing
contents of the file are destroyed. To keep the existing content, open for
Append:

Open FName For Append Access Write As #FNum

This will cause the data to be written at the end of the existing contents.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Chip Pearson" wrote in message
...
Try code like the following to get started:

Dim FName As String
Dim FNum As Integer
Dim S As String

FName = "C:\Test\TestFile.txt"
FNum = FreeFile
S = "Write This"
Open FName For Output Access Write As #FNum
Print #FNum, S
Close #FNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"JohnJ" wrote in message
...
Is there any Excel VBA code to save strings to a text file?







All times are GMT +1. The time now is 11:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com