Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Mon, 05 Nov 2007 12:58:35 -0800,    group: microsoft.public.word.vba.beginners        back       


How to save messages to a log file?   
I am new to VBA coding. I need to save messages to a log file so I can
monitor my Macro's process by the messages in the log file. Can anyone
give me a sample code to save messages to a file?

Thanks.

-Jojo
date: Mon, 05 Nov 2007 12:58:35 -0800   author:   Jojo

Re: How to save messages to a log file?   
Jojo wrote:
> I am new to VBA coding. I need to save messages to a log file so I can
> monitor my Macro's process by the messages in the log file. Can anyone
> give me a sample code to save messages to a file?
>
> Thanks.
>
> -Jojo

Sub demo()
' write messages to a "log file"
'
' This is based on the example in the VBA Help
' for the FreeFile function.

    Dim FileNumber As Integer
    Dim FileName As String

    ' Get unused file number
    FileNumber = FreeFile

    ' Construct file name based on date, time
    FileName = "C:\temp\test_" & _
        Format(Now, "yyyyMMdd_hhmm") & ".log"

    ' Open the log file
    Open FileName For Output As #FileNumber

    ' While your macro does some work,
    ' write messages to the file like this:
    Write #FileNumber, "This is the first message."
    ' Do some more work, write another message
    Write #FileNumber, "This is the second message."

    ' When your macro finishes, close the log
    Close #FileNumber
End Sub

The "For Output" expression means that if the file already exists, its 
contents will be cleared and replaced by what you write next. If you want to 
keep the old contents and add the new, change it to "For Append".

-- 
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so 
all may benefit.
date: Mon, 5 Nov 2007 16:37:00 -0500   author:   Jay Freedman

Re: How to save messages to a log file?   
Hi Jojo,

in addition to Jay's remarks,
you may find writing error messages to your log-file useful,
of course, only errors you can recover from.

Untested pseudocode!

on error goto myerror
myerror:
Write #FileNumber, err.description, err.number
resume


-- 
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
date: Tue, 06 Nov 2007 12:02:37 +0100   author:   Helmut Weber

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us