VBA ExcelWrite to Output File w/out Overwriting Everytime?

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    File Output
Click For Summary

Discussion Overview

The discussion revolves around a coding challenge in VBA for Excel, specifically how to save output to a text file without overwriting existing files. Participants explore methods to increment file names automatically with each execution of the code.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant shares their existing code for saving output to a text file and seeks advice on how to modify it to avoid overwriting files by appending a counter to the filename.
  • Another participant provides a code snippet that implements a counter to increment the filename, suggesting it can be modified for the user's needs.
  • A participant suggests that the counter should be reset after reaching a certain point, although this is met with questions about the necessity of such a reset.
  • Further discussion includes the need for a method to save the counter's value to prevent it from resetting to 1 each time the application starts.
  • Another participant proposes an alternative method that avoids the need to save the counter by checking for existing files and incrementing until an unused filename is found.

Areas of Agreement / Disagreement

Participants express differing views on whether the counter should be reset and the best method for managing file naming without overwriting. No consensus is reached on these points.

Contextual Notes

There are unresolved considerations regarding how to persist the counter value across application restarts and the implications of resetting the counter on file management.

Saladsamurai
Messages
3,009
Reaction score
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
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
 
Code:
Static Counter As Integer
Counter = Counter + 1
MyPath = "Z:\Casey B\MathML Converter\" & "MathMLAsString" & LTrim(Str(Counter)) & ".txt"
 
The above works but you should zero the counter after a certain point. Right? (At least I would)

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

Thanks
Matt
Why?
 
To reset your report numbers back to one. Why else?
 
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.
 
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
 

Similar threads

  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K