Vba 转换函数以给出第三种状态
将以下函数转换为0或1会很容易吗?它会给出以下三个输出: 0-表示文件已关闭 1-表示文件已打开 2-表示文件不存在 下面是函数Vba 转换函数以给出第三种状态,vba,excel,Vba,Excel,将以下函数转换为0或1会很容易吗?它会给出以下三个输出: 0-表示文件已关闭 1-表示文件已打开 2-表示文件不存在 下面是函数 Function IsFileReadOnlyOpen(FileName As String) Dim iFilenum As Long Dim iErr As Long On Error Resume Next iFilenum = FreeFile() Open FileName For Input Lock Read As #iFilenum Close i
Function IsFileReadOnlyOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileReadOnlyOpen = 0
Case 70: IsFileReadOnlyOpen = 1
Case Else: Error iErr
End Select
End Function
您可以在函数的开头添加以下内容:
If Dir(FileName) = "" Then 'File does not exist
IsFileReadOnlyOpen = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
我同意您应该使用enum使其更易于理解的意见
PS:正如本文所评论的,这可能会引起问题。或者,您可以使用以下选项:
With New FileSystemObject
If .FileExists(FileName) Then
IsFileReadOnlyOpen = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
End With
您可以在函数的开头添加以下内容:
If Dir(FileName) = "" Then 'File does not exist
IsFileReadOnlyOpen = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
我同意您应该使用enum使其更易于理解的意见
PS:正如本文所评论的,这可能会引起问题。或者,您可以使用以下选项:
With New FileSystemObject
If .FileExists(FileName) Then
IsFileReadOnlyOpen = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
End With
如果您有困难,可以使用FileSystemObject显式测试文件是否存在 不过,为了做到这一点,您需要添加对Microsoft脚本运行库的引用,我倾向于尽量避免这样做
您可以使用Win32API中的FindFirstFile来测试这一点,但这涉及的内容要多一些——如果用户实际运行在Mac上,也不会对您有所帮助……如果您有困难,您可以使用FileSystemObject显式测试文件是否存在 不过,为了做到这一点,您需要添加对Microsoft脚本运行库的引用,我倾向于尽量避免这样做 您可以使用Win32API中的FindFirstFile来测试这一点,但这涉及的内容稍微多一些,而且如果用户实际在Mac上运行,也不会对您有所帮助…最终导致:
Enum FileOpenState
ExistsAndClosed = 0
ExistsAndOpen = 1
NotExists = 2
End Enum
Function IsFileReadOnlyOpen(FileName As String)
With New FileSystemObject
If Not .FileExists(FileName) Then
IsFileReadOnlyOpen = 2 ' NotExists = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
End With
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileReadOnlyOpen = 0
Case 70: IsFileReadOnlyOpen = 1
Case Else: IsFileReadOnlyOpen = 1 'Error iErr
End Select
End Function
结果是:
Enum FileOpenState
ExistsAndClosed = 0
ExistsAndOpen = 1
NotExists = 2
End Enum
Function IsFileReadOnlyOpen(FileName As String)
With New FileSystemObject
If Not .FileExists(FileName) Then
IsFileReadOnlyOpen = 2 ' NotExists = 2
Exit Function 'Or not - I don't know if you want to create the file or exit in that case.
End If
End With
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileReadOnlyOpen = 0
Case 70: IsFileReadOnlyOpen = 1
Case Else: IsFileReadOnlyOpen = 1 'Error iErr
End Select
End Function
只是一个简短的评论,但是对于这类事情,最好使用枚举。。。它们使您的代码更易于阅读,特别是其他开发人员。只是一个简短的评论,但您最好使用枚举来完成这类工作。。。它们使您的代码更易于阅读,特别是其他开发人员。关于使用Dir()我唯一要发出的警告是,您不能在循环中使用它,因为它已经被用于其他目的。我以前曾写过VB6,最后编写了一些方法,这些方法被送到WINAPI中,以绕过这个小小的限制…@assylias:Martin的观点是,函数无法知道是否是从使用Dir()的循环中调用的。我们无法知道OP是否会这样使用它。Dir()的全局作用域特性可能会导致很难诊断错误……我已经说过了。Martin的观点非常重要,因为该函数将在隔离状态下正常工作。该错误不会出现在函数中,但会出现在调用过程中,在调用过程中,可能不清楚该函数是否是问题的真正原因。@mwolf02谢谢您的评论-我已修改了我的答案。关于使用Dir()我将发出的唯一警告就是不能在循环中使用它,因为它已经被用于其他目的。我以前曾写过VB6,最后编写了一些方法,这些方法被送到WINAPI中,以绕过这个小小的限制…@assylias:Martin的观点是,函数无法知道是否是从使用Dir()的循环中调用的。我们无法知道OP是否会这样使用它。Dir()的全局作用域特性可能会导致很难诊断错误……我已经说过了。Martin的观点非常重要,因为该函数将在隔离状态下正常工作。该错误不会出现在函数中,而是出现在调用过程中,在调用过程中,可能无法明显看出该函数是否是问题的真正原因。@mwolf02谢谢您的评论-我已修改了我的答案。