VBA ExcelWrite to Output File w/out Overwriting Everytime?

  • Thread starter Saladsamurai
  • Start date
  • Tags
    File Output
In summary: The code saves the text file as a newer version everytime it is run. The counter can be reset to 1 by zeroing it after a certain point.
  • #1
Saladsamurai
3,020
7
Okay then :smile:

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

Code:
    '   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?

Thanks,
Casey
 
Technology news on Phys.org
  • #2
Hello,

Here is one that I wrote to stop the overwriting. This routine is to send data from Access to Excel. It does the increment by 1 method you were looking for above. Just modify it however you like.

Private Sub cmd_Export_Click()
Dim objApp As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim Path As String

Path = "I:\Access2000\Excel Spreadsheets\W Shape Properties.xls"
Set objBook = Workbooks.Add(Template:=Path) 'Your excel spreadsheet file goes here
Set objApp = objBook.Parent
Set objSheet = objBook.Worksheets("Sheet1") 'Name of sheet you want to View
objBook.Windows(1).Visible = True
objApp.Visible = True

End Sub

Thanks
Matt
 
  • #3
Code:
Static Counter As Integer
Counter = Counter + 1
MyPath = "Z:\Casey B\MathML Converter\" & "MathMLAsString" & LTrim(Str(Counter)) & ".txt"
 
  • #4
The above works but you should zero the counter after a certain point. Right? (At least I would)

Thanks
Matt
 
  • #5
CFDFEAGURU said:
The above works but you should zero the counter after a certain point. Right? (At least I would)

Thanks
Matt
Why?
 
  • #6
To reset your report numbers back to one. Why else?
 
  • #7
Well, I guess that would be fine whenever he wants to overwrite his files.

edit:
Also, he needs a way to save the counter, otherwise it will reset to 1 each time the application starts.
 
  • #8
Here's another method that does not require the Counter to be saved.

Code:
Counter = 0
Do
    Counter = Counter + 1
    MyPath = "Z:\Casey B\MathML Converter\" & "MathMLAsString" & LTrim(Str(Counter)) & ".txt"
    If Dir(MyPath) = "" Then Exit Do
Loop
fNum = FreeFile()
Open MyPath For Output As fNum
Print #fNum, EntireMLText
Close #fNum
 

1. How can I write to an output file in VBA Excel without overwriting the existing data?

In order to write to an output file without overwriting every time, you can use the "Append" mode instead of the "Write" mode. This will add new data to the end of the existing data in the file instead of replacing it.

2. Can I specify the location of the output file when using VBA Excel to write data?

Yes, you can specify the location of the output file by using the full file path in the VBA code. Alternatively, you can use VBA to prompt the user to select the location and file name before writing the data.

3. How do I prevent the output file from becoming too large when continuously writing data to it in VBA Excel?

You can prevent the output file from becoming too large by regularly clearing the contents of the file before adding new data. You can also set a limit for the file size and create a new file once this limit is reached.

4. Is it possible to write different types of data, such as numbers and strings, to the same output file in VBA Excel?

Yes, it is possible to write different types of data to the same output file in VBA Excel. You can use the "Print" or "Write" functions to write different types of data to the file, and they will be automatically converted to the appropriate format.

5. How can I ensure that the data being written to the output file is formatted correctly in VBA Excel?

You can use the "Format" function in VBA Excel to specify the format of the data being written to the output file. This will ensure that the data is written in the desired format, such as currency or date format.

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
793
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Computing and Technology
Replies
30
Views
2K
Back
Top