我有一个创建文件的 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