VBA:Sub写入日志文件

时间:2014-03-25 20:04:06

标签: vba excel-vba excel

我在工作簿中定义了一组宏,我想为用户提供在日志文件中记录与这些宏相关的事件的选项。

我通过在ThisWorkbook中创建以下内容来启动日志:

Public writeLog as Boolean
Public logWrite as Object
Public log as Object
Private Sub Worksheet_Open()
    Dim prompt as Integer
    prompt = MsgBox("Would you like to log events for this session?", vbYesNo, "Log Events?")
    If prompt Then
        writeLog = True
        Set logWrite = CreateObject("Scripting.FileSystemObject")
        Set log = logWrite.CreateTextFile("C:/TEST.txt", False)
    Else
        writeLog = False
    End If
End Sub

然后我创建了一个程序,我可以用它来写一个这个对象的参数,我已经存储在它自己的模块中:

Public Sub PrintLog(obj as Object, argument as String)
    If writeLog = True Then
        obj.WriteLine argument
    End If
End Sub

不幸的是,这不起作用,我不确定原因:即使我没有将obj作为参数包含在函数中(因为loglogWrite被创建为全局变量),我无法Call WriteLog("String here.")Call WriteLog(log, "String here.")没有错误(Compile Error: Argument Not Optional.

是否可以使这样的Sub()起作用,以便我可以从工作簿中的任何位置调用它(例如,在用户窗体中按下按钮之后),而不必定义新的Scripting.FileSystemObject 1}}在每个模块中?

3 个答案:

答案 0 :(得分:5)

我认为您可以通过对代码进行一些小的更改来解决您的问题。我尝试了以下设置:

记录器模块:

Option Explicit

Private log As Object

Public Sub initLog()

    Dim prompt As VbMsgBoxResult
    Dim fso As Object

    prompt = MsgBox("Would you like to log events for this session?", vbYesNo, "Log Events?")

    If prompt = vbYes Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set log = fso.CreateTextFile("C:/TEST.txt", False)
    End If

End Sub

Public Sub PrintLog(argument As String)
    If Not log Is Nothing Then
        log.WriteLine argument
    End If
End Sub

Public Sub yadda()
    'test
    PrintLog "yadda"
End Sub 

的ThisWorkbook:

Private Sub Workbook_Open()
    initLog
End Sub

答案 1 :(得分:0)

我相信你会遇到问题,因为writeLog已作为布尔值存在。应该弹出错误"检测到不明确的名称"

尝试以下方法,

Public bLog as Boolean
Public logWrite as Object
Public log as Object
Private Sub Worksheet_Open()
    Dim prompt as Integer
    prompt = MsgBox("Would you like to log events for this session?", vbYesNo, "Log Events?")
    If prompt Then
        bLog = True
        Set logWrite = CreateObject("Scripting.FileSystemObject")
        Set log = logWrite.CreateTextFile("C:/TEST.txt", False)
    Else
        bLog = False
    End If
End Sub

Public Sub WriteLog(Optional obj as Object, Optional argument as String)
    If bLog = True Then
        obj.WriteLine argument
    End If
End Sub

编辑:在WriteLog(或PrintLog)中使参数可选,以便进一步测试

答案 2 :(得分:0)

' Write to a log file using Separator and Array of variant Parameters
' Auto generate the file
' USE EndLog to close    

'use: 
' PrintLog vbtab, "one", 2, 3
' PrintLog vbtab, "Apple","Windows","Linux","Android","Commodore","Amiga","Spectrum"
' EndLog

' Generate a csv file:
' PrintLog ";", rst!ID, rst!Name

Private FileLog As Object
Private fso As Object
Const DEBUG_LOG_FILE = "C:\log.txt"

Public Sub PrintLog(ByVal Separator As String, ParamArray Arguments() As Variant)
    Dim ele As Variant
    Dim line As String
    
    If FileLog Is Nothing Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set FileLog = fso.CreateTextFile(DEBUG_LOG_FILE, True, True)
    End If
    
    line = CStr(Now()) ' Print Timestamp

    For Each ele In Arguments
        If line > "" Then line = line & Separator
        line = line & CStr(ele)
    Next
    If line > "" Then FileLog.WriteLine line
End Sub

Public Sub EndLog()
    On Error Resume Next
    FileLog.Close
    Set FileLog = Nothing
    Set fso = Nothing
    On Error GoTo 0
End Sub