How do I create a log file for a macro in Word?

M

MRMichaels

I have been using a macro regularly that creates Access data tables out of
locked Word forms. Recently I the macro has started to have difficulty
working on some machines and with some documents. I am having trouble
diagnosing the program. How do I make VB write a log file when I run a macro?
 
R

RB Smissaert

See if something like this suits you:


Sub test()

Dim bt As Byte
Dim btSum As Byte
Dim arrErrorData(1 To 6)

10 On Error GoTo ERROROUT

20 For bt = 1 To 300
30 btSum = btSum + bt
40 Next

50 Exit Sub
ERROROUT:

60 arrErrorData(1) = Format(Now, "dd/mmm/yyyy hh:mm")
70 arrErrorData(2) = "Module: Module1"
80 arrErrorData(3) = "Procedure: Sub test()"
90 arrErrorData(4) = "Error line: " & Erl
100 arrErrorData(5) = "Error number: " & Err.Number
110 arrErrorData(6) = Err.Description

120 Save1DArrayToTextAppend "C:\ErrorqqLog.txt", arrErrorData

End Sub


Sub Save1DArrayToTextAppend(txtFile As String, _
arr As Variant, _
Optional LB As Long = -1, _
Optional UB As Long = -1)

Dim r As Long
Dim hFile As Long

If LB = -1 Then
LB = LBound(arr)
End If

If UB = -1 Then
UB = UBound(arr)
End If

hFile = FreeFile

Open txtFile For Append As hFile

For r = LB To UB
If r = UB Then
Write #hFile, arr(r)
Else
Write #hFile, arr(r);
End If
Next

Close #hFile

End Sub


Just copy and paste in a module and run the Sub test.


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top