Public Function WriteFile(ByVal strFileName As String, colWri As Collection, Optional bOverLay As Boolean = False, Optional bMarkSeq As Boolean = False) As Boolean
Dim FN
Dim v
Dim strLine As String
Dim Seq As Long
Dim strPath As String
Dim Pos As Integer
'保证路径存在
Pos = InStrRev(strFileName, "\")
If Pos > 0 Then
strPath = Left(strFileName, Pos - 1)
If Not NewDir(strPath) Then
SysErr "路径" & vbCrLf & strPath & vbCrLf & "非法!"
Exit Function
End If
End If
On Error GoTo ErrExit
If bOverLay Then KillFile strFileName
FN = FreeFile()
Open strFileName For Append As #FN
Seq = 1
For Each v In colWri
strLine = v
If bMarkSeq Then strLine = Seq & vbTab & strLine
Print #FN, strLine
Seq = Seq + 1
Next v
Close #FN
ErrExit:
WriteFile = (Err = 0)
End Function
Dim FN
Dim v
Dim strLine As String
Dim Seq As Long
Dim strPath As String
Dim Pos As Integer
'保证路径存在
Pos = InStrRev(strFileName, "\")
If Pos > 0 Then
strPath = Left(strFileName, Pos - 1)
If Not NewDir(strPath) Then
SysErr "路径" & vbCrLf & strPath & vbCrLf & "非法!"
Exit Function
End If
End If
On Error GoTo ErrExit
If bOverLay Then KillFile strFileName
FN = FreeFile()
Open strFileName For Append As #FN
Seq = 1
For Each v In colWri
strLine = v
If bMarkSeq Then strLine = Seq & vbTab & strLine
Print #FN, strLine
Seq = Seq + 1
Next v
Close #FN
ErrExit:
WriteFile = (Err = 0)
End Function

本文介绍了一个VBA自定义函数用于将集合中的数据写入文件。该函数支持覆盖写入及标记行序号的功能,并确保目标路径的有效性。
590

被折叠的 条评论
为什么被折叠?



