VBA Close a text file without saving

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bansod
    New Member
    • May 2020
    • 2

    VBA Close a text file without saving

    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.

    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 >>>>>>
    Last edited by gits; May 3 '20, 06:12 PM. Reason: added code tags
  • bansod
    New Member
    • May 2020
    • 2

    #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:

    Code:
    Dim FSO As Object, MyFileObj As Object
    Dim ExtDictionaryPath As String
    Dim FileArray As Variant
    Dim Buffr as String
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MyFileObj = FSO.OpenTextFile(ExtDictionaryPath, 1)
                
    Buffr = MyFileObj.Readall
    Buffr = Replace$(Buffr, vbCrLf, vbCr) ' Strip out any CRLF if file has both CR & LF, keep only CR
    Buffr = Replace$(Buffr, vbLf, vbCr)   ' Strip out any LF if file has only LF, replace with CR
    
    FileArray = Split(Buffr, vbCr) ' Split string into each word
    
    '' <<< Rest of code  >>

    Comment

    Working...