Excel 函数将调试定向到文本文件

Excel 函数将调试定向到文本文件,excel,vba,Excel,Vba,我想写一个函数,允许我在未来的模块中使用Print#Debug,“text”来收集调试语句 Sub output_debug() Dim WshShell As Object Dim Desktop As String Dim Debug As Integer Debug = FreeFile() Set WshShell = CreateObject("WScript.shell") Desktop = WshShell.specialfolders("Desktop") Open De

我想写一个函数,允许我在未来的模块中使用
Print#Debug,“text”
来收集调试语句

Sub output_debug()
Dim WshShell As Object
Dim Desktop As String
Dim Debug As Integer

Debug = FreeFile()

Set WshShell = CreateObject("WScript.shell")
Desktop = WshShell.specialfolders("Desktop")

Open Desktop & "\VBA_output.txt" For Output As #Debug
Print #Debug, "test"

Close #Debug

End Sub

我如何才能从上面开始,定义一个函数,允许我在一个模块中使用
调用output_debug()
,这样我所有的
Print#debug,
都会打印到该文件中?我想我需要创建另一个名为
close\u output()
的函数,该函数具有
close\35; Debug

尝试这样的子例程。。。 它会将文本记录到带有日期戳的文本文件中,因此新文件将在新的一天创建。 如果您在代码中捕获错误,您可以选择将ERR对象传递给它,并且它将以突出显示的方式记录错误消息

call debuglog("my log entry")

call debuglog("my log entry",err)


Public Sub DebugLog(sLogEntry As String, Optional ByVal oErr As Object)
' write debug information to a log file
Dim iFile As Integer
Dim sDirectory As String
Dim errNumber, errDescription As Variant
Dim l As Integer

If Not oErr Is Nothing Then
    errNumber = oErr.Number
    errDescription = oErr.Description
    l = IIf(Len(errDescription) > Len(sLogEntry), Len(errDescription), Len(sLogEntry))
End If

On Error GoTo bail

sfilename = VBA.Environ("Homedrive") & VBA.Environ("Homepath") & "\My Documents\Debuglog" & "\debuglog" & Format$(Now, "YYMMDD") & ".txt"
iFile = FreeFile

Open sfilename For Append As iFile

If Not oErr Is Nothing Then
    sLogEntry = "/" & String(5 + (l - Len(sLogEntry)), "-") & " " & sLogEntry & " " & String(5 + (l - Len(sLogEntry)), "-") & "\"
    Print #iFile, Now; " "; sLogEntry
    Print #iFile, Now; "  "; errNumber
    Print #iFile, Now; "  "; errDescription
    Print #iFile, Now; " "; "\" & String(Len(sLogEntry) - 2, "-") & "/"
Else
    Print #iFile, Now; " "; sLogEntry
End If

bail:
    Close iFile

End Sub
日志文件输出示例

27/03/2015 10:44:27  -- COMIT Form Initialize - Complete
27/03/2015 10:44:27  -  COMIT Active
27/03/2015 10:44:34  /----- -- Error Populating Opportunity Form: frmBluesheet.PopulateForm() -----\
27/03/2015 10:44:34    381 
27/03/2015 10:44:34   Could not get the Column property. Invalid property array index.
27/03/2015 10:44:34  \-----------------------------------------------------------------------------/

我过去做过类似的事情。这是我想到的。它依赖于在任何使用它的项目中引用
Microsoft脚本运行时
。您可以将以下SUB存储在一个模块中,例如,
DebugLogger
(这就是我使用的),该模块可以首先导出,然后导入到您希望具有此功能的任何模块中。它模仿Debug.Print的行为,但将输出发送到一个文件,该文件的名称是工作簿名称的函数。我曾尝试过在单个条目上加盖时间戳的想法,但拒绝了这个想法,因为它与
Debug.Print
的功能相去甚远(不过,我确实会在创建日期上加盖时间戳)。导入模块并建立正确的引用后,您就可以在任何地方使用
DebugPrint
。默认情况下,它也会打印到调试窗口。您可以完全删除该部分代码,也可以切换默认值

Function GetFullDebugName() As String
    'This function returns a string of the form
    '*xldebug.txt, where *.* is the full name of the workbook

    Dim MyName As String
    Dim NameParts As Variant

    MyName = ThisWorkbook.FullName
    NameParts = Split(MyName, ".")
    GetFullDebugName = NameParts(0) & "xldebug.txt"
End Function

Sub CreateDebugFile()
    'file created in same directory as
    'calling workbook

    Dim DebugName As String
    Dim fso As FileSystemObject
    Dim MyStream As TextStream

    Set fso = New FileSystemObject
    DebugName = GetFullDebugName
    Set MyStream = fso.CreateTextFile(DebugName)
    MyStream.WriteLine "This debug file was created " _
                        & FormatDateTime(Date) _
                        & " at " & FormatDateTime(Time)
    MyStream.Close
End Sub

Sub DebugLog(DebugItem As Variant, Optional ToImmediate As Boolean = True)
    Dim DebugName As String
    Dim fso As FileSystemObject
    Dim MyStream As TextStream

    Set fso = New FileSystemObject
    DebugName = GetFullDebugName
    'check to see if DebugFile exist
    'if not, create it:
    If Not fso.FileExists(DebugName) Then CreateDebugFile

    Set MyStream = fso.OpenTextFile(DebugName, ForAppending)
    MyStream.WriteLine DebugItem
    MyStream.Close
    If ToImmediate Then Debug.Print DebugItem
End Sub

你的意思是调试打印?您应该能够只传递要打印的内容的参数?
debug.print
将其打印到即时窗口。我希望将它们收集到我指定的文本文件中。对。不熟悉
Print#Debug
,但像Dave在下面做的那样,只需传递参数。
sLogEntry=“/”&String(5+(l-Len(sLogEntry)),“-”&&&sLogEntry&&&String(5+(l-Len(sLogEntry)),“-”&“,”
我不理解
String()
语句的要点。为什么我们要关心
sLogEntry
的长度。我也不明白
iif()。不是真的需要,但我发现它很有用。行中的highlight函数在哪里?
string()
不是只是将参数转换成字符串吗?它用括号将错误包装起来以突出显示行。例如,
27/03/2015 10:44:34/----填充机会表单时出错:frmBluesheet.PopulateForm()----\27/03/2015 10:44:34\---------------------------------------------------------------------------------------------------------------/
此注释中难以设置格式,请参阅修订的答案哦!我是在装傻。我把高光理解为黄色高光。这正是让我失望的地方,我找不到突出显示功能!感谢您的投入!似乎
opentextfile
实际上没有打开该文件?我必须强制记事本通过shell打开?@findwindowvba打开文本文件进行写入-这与用户在桌面上打开文本文件不同。记事本是另一种应用程序。如果希望记事本自动打开文件,则需要在写入文件后使用VBA
Shell
功能。看这个:对。似乎有点愚蠢,必须使它成为一个两步的过程XD