vba 输出到日志文件
- VBE 环境,立即窗口输出太多时,开始的输出可能被覆盖了,影响调试,所以输出到文件,可以详细输出
Public LogFilePath As String
Sub InitLogFile()LogFilePath = Environ("USERPROFILE") & "\Desktop\VBA_Log_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".log"
End Sub
Public Sub log_print(outputToConsole As Boolean, ParamArray args() As Variant)Dim i As LongDim outputText As StringDim fileNumber As IntegerFor i = LBound(args) To UBound(args)outputText = outputText & args(i)If i < UBound(args) ThenoutputText = outputText & " " End IfNext iIf outputToConsole ThenDebug.Print outputTextEnd IfIf LogFilePath = "" ThenLogFilePath = Environ("USERPROFILE") & "\Desktop\VBA_Log_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".log"End IffileNumber = FreeFileOpen LogFilePath For Append As #fileNumberPrint #fileNumber, Now() & " | " & outputText Close #fileNumber
End Sub