473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

VBA Close a text file without saving

2
I wonder if someone can help on this question.

I want to be able to open a text file in Excel VBA, make some changes to it, use the changed data in the rest of the code, and then close without saving the file. In other words I want to leave the original file (which is a reference) untouched. I cannot find any way to do this.

What I am doing is reading an English word dictionary text file to use as a reference. The code needs to work with various dictionaries. Some dictionaries use CRLF at the end of each word, others use LF only. Those files I have to replace the LF with CR so that VBA will read each word as a separate record, if there is no CR the entire file is read as one big record.

Here is my code. It works fine except for the problem that the changed dictionary gets saved. Any help on this is greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. '' First condition the dictionary to avoid reading entire file as 1 record.
  2. '' If the file has no CR after each word the entire file is read as a single record, so that needs to be fixed
  3. '' CRs are required between each word to properly read each word as a separate record
  4. '' If the dictionary file has no CR and only LF between each word, replace the LF with CR
  5. '' LFs are ignored, so CRLF is OK, CR is OK, but only LF is not, there must be a CR.
  6.  
  7. MyFile = FreeFile() ' finds next available file number
  8. Open ExtDictionaryPath For Binary As MyFile
  9.  
  10. Buff = String$(LOF(MyFile), 0) ' Fill Buff with zero string with length = length of file in bytes
  11. Get MyFile, , Buff ' Read entire file as 1 record into Buff
  12. Buff = Replace$(Buff, vbCrLf, vbCr) ' Strip out any CRLF if file has both CR & LF, keep only CR
  13. Buff = Replace$(Buff, vbLf, vbCr) ' Strip out any LF if file has only LF, replace with CR
  14. Put MyFile, 1, Buff ' Write back to file starting at byte number 1
  15.  
  16. Close MyFile
  17.  
  18. ' read in words from dictionary 1 line at a time
  19. MyFile = FreeFile() ' finds next available file number
  20. Open ExtDictionaryPath For Input As MyFile ' Opens dictionary file
  21.  
  22. While Not EOF(MyFile)
  23.  
  24. Line Input #MyFile, FoundLegalWord ' reads each line of dictionary into string FoundLegalWord
  25.  
  26. ‘’’ <<<< rest of processing >>>>>>
May 3 '20 #1
1 3027
bansod
2
I think I've solved my own problem.

Instead of opening the file, modifying it, saving it and then reading it back, I am just reading the file into an array and making modifications in the array before using it further in the program. I never write anything back to the file and it never changes.

Here is the new code:

Expand|Select|Wrap|Line Numbers
  1. Dim FSO As Object, MyFileObj As Object
  2. Dim ExtDictionaryPath As String
  3. Dim FileArray As Variant
  4. Dim Buffr as String
  5.  
  6.  
  7. Set FSO = CreateObject("Scripting.FileSystemObject")
  8. Set MyFileObj = FSO.OpenTextFile(ExtDictionaryPath, 1)
  9.  
  10. Buffr = MyFileObj.Readall
  11. Buffr = Replace$(Buffr, vbCrLf, vbCr) ' Strip out any CRLF if file has both CR & LF, keep only CR
  12. Buffr = Replace$(Buffr, vbLf, vbCr)   ' Strip out any LF if file has only LF, replace with CR
  13.  
  14. FileArray = Split(Buffr, vbCr) ' Split string into each word
  15.  
  16. '' <<< Rest of code  >>
May 5 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AaronV | last post by:
I've seen a few sites that offer this, but I'm not sure how they do it. It's a form button, that when pressed, prompts the use to download a file. Like if I want my banking records, I select a date...
2
by: Jeffrey | last post by:
What classes/methods can I use to do the following? 1. Open a text file (less than 50k in size) 2. Replace some text in it. 3. Close the text file. I don't want to have to create a new file...
6
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
4
by: mvvdsteen | last post by:
Hello all, I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards...
1
by: shakai | last post by:
Hi I have text file. The columns in the text file are separated by whitespaces.This text file consists of the floatig point values. I want to save each column in the separate array and find the...
3
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
2
TheSmileyCoder
by: TheSmileyCoder | last post by:
I have a form, which I open in dialog mode, and through the openArgs I set some values in the form (in a new record) thus dirtying the form. The idea is then that the user should make a selection...
4
by: GeneticsJustin | last post by:
Hi everyone, I'm new to python and would like to split a FASTA (text) file into each different gene (separated by a ">"), randomly sample a certain number of the sequences, and print the result. I...
3
by: Paul Howarth | last post by:
I am exporting a Query to a Text File. One of my fields is the current Date. F1:Date() When I look at the text file, my date is displaying the System Date which includes the time. Example:...
0
by: topu | last post by:
Write the number of days in months program so that the program prompts the user for the file name and reads the dates from the file. The dates in the file are written in the format dd.mm.yyyy....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.