1

我有一个创建文件的 VBA 代码,并且需要有一个错误处理例程,以防文件关闭(如果它实际上是打开的)。

如果文件在到达 Close #F之前或什至在实际创建之前已打开并崩溃。

Sub test1()
On Error GoTo Err

    Dim rs As DAO.Recordset
    Dim F As Integer

    F = FreeFile

    Open "C:\MYFILE.TXT" For Output As #F

    ' My code that does lots of stuff...

    If Not rs Is Nothing Then rs.Close
    Close #F

Exit_Sub:
    Set rs = Nothing
    Exit Sub
Err:
    MsgBox Err.Description
    Resume Exit_Sub 

End Sub
4

1 回答 1

3

改变这部分

Exit_Sub:
    Set rs = Nothing
    Exit Sub

Exit_Sub:
    On Error Resume Next
    Close #F
    On Error GoTo 0
    Set rs = Nothing
    Exit Sub

Close #F从之前的位置删除...

于 2012-05-14T21:35:25.533 回答