PC Review


Reply
Thread Tools Rate Thread

Code for running macro on save

 
 
santaviga
Guest
Posts: n/a
 
      23rd Jan 2008
Hi,

I have a macro that when run exports the data in the workbook to a text
file, is there anything I can add to this code so that when I press save it
automatically runs the macro and saves a version of the text file to my
desktop. The code I have is below.

Regards

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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop Macro running when I save the workbook TyreDude Microsoft Excel Worksheet Functions 1 9th Jul 2011 12:35 AM
How can I save the formulas when running a macro? Need ASAP!! =?Utf-8?B?TUxQ?= Microsoft Excel Misc 2 14th Nov 2006 07:50 PM
Save To Csv Running A Macro stevekirk Microsoft Excel Misc 2 11th Aug 2006 02:42 PM
Save Status Bar Disappers after running macro =?Utf-8?B?RXhjZWxNb25rZXk=?= Microsoft Excel Programming 1 15th Jun 2006 12:52 AM
When Save As stop running macro M K W Microsoft Excel Programming 3 2nd Sep 2003 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:21 AM.