PDA

View Full Version : Write to Output File w/out Overwriting Everytime?



Saladsamurai
08-19-2009, 07:23 AM
Okay then :)

Here is a piece of the code that I am using:




' Send to File
MyPath = "Z:\Casey B\MathML Converter\" & "MathMLAsString.txt"
fNum = FreeFile()
Open MyPath For Output As fNum
Print #fNum, EntireMLText
Close #fNum

This saves my work to a text file called "MathMLAsString"
What I would like to do is set it up so that everytime a run the code it saves the text file as a newer version so I can compare text files (i.e. MathMLAsString is not overwritten).

Something like MathMLAsString1.txt, MathMLAsString2.txt, etc...

I was thinking of starting a counter, but I do not know how VBA will know to increment it every time the code is run?


Any ideas?

Bob Phillips
08-19-2009, 07:50 AM
Two options -

- Save the counter as a name within the workbook you are using

- write it to the registry

Saladsamurai
08-19-2009, 07:55 AM
Two options -

- Save the counter as a name within the workbook you are using

- write it to the registry

Hi there xld :)

Perhaps you could elaborate on either one of those, since I do not seem to follow what you are saying? :dunno

Benzadeus
08-19-2009, 08:22 AM
What about saving date and time every time that run the macro?

MyPath = "Z:\Casey B\MathML Converter\" & "MathMLAsString.txt " & Replace(Replace(Now, ":", "-"), "/", "-")

Bob Phillips
08-19-2009, 08:26 AM
This will create and maintain a name called IncNum and drop it into a public variable called myNum, which you can append to the name



Public myNum As Long

Public Sub IncrementValue()

On Error Resume Next
myNum = Application.Evaluate(ThisWorkbook.Names("IncNum").RefersTo)
On Error GoTo 0
myNum = myNum + 1
ThisWorkbook.Names.Add Name:="IncNum", RefersTo:=myNum

End Sub

mdmackillop
08-19-2009, 11:11 AM
To use the Registry, look at this (http://www.vbaexpress.com/kb/getarticle.php?kb_id=208)

Benzadeus
08-19-2009, 12:41 PM
xld, couldn't you use just

myNum = ThisWorkbook.Names("IncNum").RefersTo

instead of
myNum = Application.Evaluate(ThisWorkbook.Names("IncNum").RefersTo) ?

Bob Phillips
08-19-2009, 01:55 PM
Yeah, I think you are right.