PDA

View Full Version : Create Text Files for each cell value



uaku
01-04-2012, 11:02 AM
I am creating page-a-day calendar and for that I need to put text for each day into a single text file.
I was wondering if it is possible that I create a spreadsheet with 365 rows and each row value is saved as 1.txt , 2.txt, ..... upto 365.txt

I would appreciate your help

mdmackillop
01-04-2012, 01:18 PM
This will create 365 text files in the format 001.txt (allows for sorting in ascending order)
Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f, i as Long
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 1 To 365
Set f = fs.OpenTextFile("c:\aaa\" & Format(i, "000") & ".txt", 1, -1)
f.Close
Next i
End Sub

uaku
01-04-2012, 01:41 PM
This will create 365 text files in the format 001.txt (allows for sorting in ascending order)
Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f, i as Long
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 1 To 365
Set f = fs.OpenTextFile("c:\aaa\" & Format(i, "000") & ".txt", 1, -1)
f.Close
Next i
End Sub


Thanks for your quick reply. In the above code, I did not see the program picking cell values from given row. I was thinking to put all the text in 365 rows and want the program to pick each cell value paste in a text file and save it as 1.txt, 2.txt etc.....

mdmackillop
01-04-2012, 02:23 PM
I think I see whay you are after. We can easily create all the files. Will this be run every day?, what about previous data etc. Is each column in a row in separate line in each file or what?

uaku
01-04-2012, 02:33 PM
I think I see whay you are after. We can easily create all the files. Will this be run every day?, what about previous data etc. Is each column in a row in separate line in each file or what?

No it will not run every day. I will use LaTeX to produce PDF with 365 files created using VBA, and each day containing a saying or a sentence of the language I want to learn. I am trying to make a custom page-a-day calendar
Unless there is a way to create a pdf via VBA.

Kenneth Hobs
01-04-2012, 02:39 PM
You can put the contents of each cell into a worksheet and save that as a PDF. Create a scratch worksheet.

Example:

'http://www.mrexcel.com/forum/showthread.php?p=2850609
Sub Test_PublishToPDF()
Dim sDirectoryLocation As String, sName As String

sDirectoryLocation = ThisWorkbook.Path
sName = sDirectoryLocation & "\" & Range("E4").Value2 & ".pdf"
PublishToPDF sName, ActiveSheet
End Sub


Sub PublishToPDF(fName As String, ws As Worksheet)
Dim rc As Variant

'ChDrive "c:"
'ChDir GetFolderName(fName)
rc = Application.GetSaveAsFilename(fName, "PDF (*.pdf), *.pdf", 1, "Publish to PDF")
If rc = "" Then Exit Sub

ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub