ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help - Can Excel create a text file?? (https://www.excelbanter.com/excel-programming/348404-help-can-excel-create-text-file.html)

Acube

Help - Can Excel create a text file??
 

Hi, Can we programme excel to create a Text file in the folder using
VBA? For example: I want to programme VBA to create a file call
Mysave.txt inside one of the folder? Can that be done?? Pls help me...
Thanks alot..


--
Acube
------------------------------------------------------------------------
Acube's Profile: http://www.excelforum.com/member.php...o&userid=29734
View this thread: http://www.excelforum.com/showthread...hreadid=494451


Chip Pearson

Help - Can Excel create a text file??
 
Try something like the following:


Dim FName As String
Dim Ndx As Long
FName = "H:\Test\test.txt" '<< CHANGE
Open FName For Output As #1
For Ndx = 1 To 10
Print #1, Ndx
Next Ndx
Close #1


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Acube"
wrote in message
...

Hi, Can we programme excel to create a Text file in the folder
using
VBA? For example: I want to programme VBA to create a file call
Mysave.txt inside one of the folder? Can that be done?? Pls
help me...
Thanks alot..


--
Acube
------------------------------------------------------------------------
Acube's Profile:
http://www.excelforum.com/member.php...o&userid=29734
View this thread:
http://www.excelforum.com/showthread...hreadid=494451




Edward Ulle

Help - Can Excel create a text file??
 
If you want to be a little more elegant this will present the typical
windows file dialog box. The user can input a new file name and then
write to the text stream.

Just presenting another option. Notice you have to refence the
Microsoft Scripting Runtime and I typically close the text stream before
exiting the module.

Option Explicit

' Requires Reference to Microsoft Scripting Rntime

Sub Test()

Dim fsoFileSystemObject As FileSystemObject
Dim strFileName As String
Dim tsTextStream As TextStream

Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")
strFileName = Application.GetSaveAsFilename()
Set tsTextStream = fsoFileSystemObject.CreateTextFile(strFileName)

tsTextStream.WriteLine "Line 1"
tsTextStream.WriteLine "Line 2"
tsTextStream.WriteLine "Line 3"

tsTextStream.Close

End Sub




*** Sent via Developersdex http://www.developersdex.com ***

[email protected]

Help - Can Excel create a text file??
 
Try this
ActiveCell.FormulaR1C1 = Range("A1")
ActiveCell.FormulaR1C1 = Range("A2")

Open "C\text.txt" For Append As #1
Write #1, Range("A1"),1Range("A2")
Close #1



All times are GMT +1. The time now is 05:16 PM.

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