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.
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.
Code:
'' First condition the dictionary to avoid reading entire file as 1 record. '' If the file has no CR after each word the entire file is read as a single record, so that needs to be fixed '' CRs are required between each word to properly read each word as a separate record '' If the dictionary file has no CR and only LF between each word, replace the LF with CR '' LFs are ignored, so CRLF is OK, CR is OK, but only LF is not, there must be a CR. MyFile = FreeFile() ' finds next available file number Open ExtDictionaryPath For Binary As MyFile Buff = String$(LOF(MyFile), 0) ' Fill Buff with zero string with length = length of file in bytes Get MyFile, , Buff ' Read entire file as 1 record into Buff Buff = Replace$(Buff, vbCrLf, vbCr) ' Strip out any CRLF if file has both CR & LF, keep only CR Buff = Replace$(Buff, vbLf, vbCr) ' Strip out any LF if file has only LF, replace with CR Put MyFile, 1, Buff ' Write back to file starting at byte number 1 Close MyFile ' read in words from dictionary 1 line at a time MyFile = FreeFile() ' finds next available file number Open ExtDictionaryPath For Input As MyFile ' Opens dictionary file While Not EOF(MyFile) Line Input #MyFile, FoundLegalWord ' reads each line of dictionary into string FoundLegalWord ‘’’ <<<< rest of processing >>>>>>
Comment