Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have the following code in my workbook, it allows me to run the macro to
export all the data to a text file, How can I include a code into this to export the data to a text file when I press the save button on excel. The code I have is below. Regrads Sub WriteFixed() Const MyPath = "C:\documents and settings\Mark\desktop\" Const WriteFileName = "Trial1.txt" Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Set fswrite = CreateObject("Scripting.FileSystemObject") 'open files WritePathName = MyPath + WriteFileName fswrite.CreateTextFile WritePathName Set fwrite = fswrite.GetFile(WritePathName) Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault) LastRow = Cells(Rows.Count, "A").End(xlUp).Row For RowCount = 1 To LastRow LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column OutPutLine = "" For Colcount = 1 To LastCol Data = Trim(Cells(RowCount, Colcount).Text) If Len(Data) < 12 Then Data = Data & WorksheetFunction.Rept(" ", 12 - Len(Data)) Else Data = Left(Data, 12) End If If OutPutLine < "" Then OutPutLine = OutPutLine & " " End If OutPutLine = OutPutLine & Data Next Colcount tswrite.writeline OutPutLine Next RowCount tswrite.Close End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How to code the Macro to save and close a file? | Excel Discussion (Misc queries) | |||
Macro Save As Code??? | Excel Worksheet Functions | |||
Macro VBA code to name Save-As file | Excel Worksheet Functions | |||
Totally Disabling (^ save ) (Save as) and Save Icon – Which code do I use: | Excel Programming | |||
Placing a code before Save & Save As | Excel Programming |