输出ListBox作为多行日志

时间:2016-10-12 03:33:11

标签: vba

在自动执行数据库上的某些错误检查过程时遇到了问题。我需要输出一个与ListBox的输出相同的日志,但是我的当前(FreeFile)方法将在每次向该框添加新行时覆盖日志文件。

Sub ExampleString (s As String)
Dim n as Object

n = FreeFile()
Open "C:\Path\TEST.txt" For Output As #n

If Not listBoxOut Is Nothing Then
With listBoxOut
' add items to List Box
Print #n, s
' iterate through List Box
Close #n
End With
End If

End Sub

我如何以与在多个行中在ListBox中完成的方式相同的方式输出此文本?

谢谢,

Neuw

1 个答案:

答案 0 :(得分:0)

仅处理打印文件语句,您必须:

  • 将n类型更改为integer
  • 使用Append关键字

这是修改后的代码

Sub ExampleString(s As String)
    Dim n As Integer

    n = FreeFile()
    Open "C:\Path\TEST.txt" For Append As #n

    If Not listBoxOut Is Nothing Then
        With listBoxOut
            ' add items to List Box
            Print #n, s
            ' iterate through List Box
            Close #n
        End With
    End If
End Sub