关闭#1 - 强制使用Inelegant代码。为什么?

时间:2018-05-12 16:03:41

标签: vba input output

我会喜欢这个方向......

我想使用此代码:

Do While x <> y

    Open myFile.txt For Input As #1
    Open anotherFile.txt For Output As #2

        'doStuff

    Write #2, myString
    Close #1
    Close #2

    Open anotherFile.txt For Input As #3    'Note: same file as #2 above

        'do stuff
    print #3, myString   
    Close #3

Loop

上面的代码偶尔抛出:“运行时错误52.数字文件名错误。”当试图打开#3时

然而,只需点击简历就可以继续进行干预

我必须使用的是:

Do While x <> y

    Open myFile.txt For Input As #1
    Open anotherFile.txt For Output As #2

        'doStuff

    Write #2, myString
    Close #1
    Close #2

    On Error Resume Next
        Close #3             'YES, CLOSE AGAIN
        Sleep 250           'GIVE IT TIME TO ACTUALL CLOSE

        'ATTEMPT TO OPEN  NOTE, SAME FILE AS #2 ABOVE
        Open anotherFile.txt For Input As #3
        If Err.Number <> 0 Then   'CHECK FOR THE ERROR
            Close #3   'ISSUE ANOTHER CLOSE
            Err.Number = 0
            Sleep 250  'GIVE IT SOME TIME AGAIN
            Open anotherFile.txt For Input As #3 'ATTEMPT TO OPEN AGAIN
            If Err.Number <> 0 Then  'ERROR CHECK AGAIN
                MsgBox "Repeated Error " & Err.Description
                Stop
            End If
        End If
    On Error GoTo 0

        'do stuff
    Print #3, myString
    Close #3

Loop

幸运的是,它永远不会超过重复的错误检查。尽管如此,我不喜欢使用这种草率的代码。

有关于此的任何想法吗?

非常感谢!

旅行

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题:

  1. 假设file ID #3 可用
  2.   

    FreeFile Function

         
        
    • 返回表示可供Open语句使用的下一个文件编号的Integer
    •   
    • 文件编号 - Open语句中用于打开文件的编号。对于其他应用程序无法访问的文件,请使用1-255范围内的文件编号。对于可从其他应用程序访问的文件,请使用256-511范围内的文件编号
    •   
         

    <强> Syntax

         
        
    • FreeFile[(rangenumber)]

           
          
      • 可选的rangenumber参数是Variant,它指定从中返回下一个空闲文件编号的范围。      
            
        • 指定 0 (默认)以返回 1 – 255 范围内的文件编号。
        •   
        • 指定 1 以返回 256 – 511 范围内的文件编号。
        •   
      •   
    •   
         

    <强> Remarks

         
        
    • 使用FreeFile提供尚未使用的文件编号。
    •   

    最有可能的原因是 "Run time error 52" Bad file name or number (Error 52)

    1. 您可能会尝试再次打开文件 "anotherFile.txt"

      有一种方法可以提高这种逻辑的效率

    2. 解决问题 1。

      Option Explicit
      
      Public Sub ProcesFiles1()
          Dim fileID As String, txt As String
      
          'Step 1 of 2 -------------------------------------------------------------------------
      
          fileID = FreeFile   'Next file number available for use by the Open statement
          Open "D:\Tmp\Test1.txt" For Input As #fileID    'Open file in a Read Mode
          txt = Input(LOF(fileID), #fileID)    'Store file content inside a variable
          Close #fileID                        'Clost Text File
      
          txt = Replace(txt, "string1", "string2")        'Step 1 of 2
      
          fileID = FreeFile
          Open "D:\Tmp\Test2.txt" For Output As #fileID   'Open file in Write Mode
          Print #fileID, txt  'Write New Text data to file
          Close #fileID       'Close Text File
      
          'Step 2 of 2 -------------------------------------------------------------------------
      
          fileID = FreeFile
          Open "D:\Tmp\Test2.txt" For Input As #fileID    'Open file2 (again) in Read Mode
          txt = Input(LOF(fileID), #fileID)
          Close #fileID
      
          txt = Replace(txt, "string2", "string3")        'Step 2 of 2
      
          fileID = FreeFile
          Open "D:\Tmp\Test2.txt" For Output As #fileID   'Open file2 (yet again) in Write Mode
          Print #fileID, txt
          Close #fileID
      End Sub
      

      风险:时间问题 - Step 1Step 2冲突

      解决问题 2。 - 合并Step 12(txt处理独立于文件对象)

      Option Explicit
      
      Public Sub ProcesFiles2()
          Dim fileID As String, txt As String
      
          fileID = FreeFile   'Next file number available for use by the FileOpen function
          Open "D:\Tmp\Test1.txt" For Input As #fileID    'Open file in a Read Mode
          txt = Input(LOF(fileID), #fileID)   'Store file content inside a variable
          Close #fileID                       'Clost Text File
      
          txt = Replace(txt, "string1", "string2")        'Step 1 of 2
          txt = Replace(txt, "string2", "string3")        'Step 2 of 2
      
          fileID = FreeFile
          Open "D:\Tmp\Test2.txt" For Output As #fileID   'Open file in Write Mode
          Print #fileID, txt  'Write New Text data to file
          Close #fileID       'Close Text File
      End Sub