Log File

G

Greg Maxey

I am working on a macro to process several files in a folder. I would like
to create a text file to log of each files opened and the results of the
processing.

I have seen something similiar but I can't seem to locate it and I don't
know where to start from scratch.

Thanks..
 
D

Doug Robbins - Word MVP

Hi Greg,

The following code will do that, saving the log file in the folder that
contained the documents that were processed:

Option Explicit
Dim myFile As String
Dim Files As Variant
Dim FileList As String
Dim i As Long, j As Long
Private Sub cmdCancel_Click()
'If Documents.Count > 0 Then
' Documents.Close Savechanges:=wdPromptToSaveChanges
'End If
Unload Me
End Sub
Private Sub cmdGo_Click()
Dim myDoc As Document
Dim Log As Document
Dim Logstr As String
Logstr = ""
myFile = Dir$(PathToUse & "*.doc")
FileList = ""
Do While myFile <> ""
FileList = FileList & myFile & "#"
myFile = Dir$()
Loop
FileList = Left(FileList, Len(FileList) - 1)
Files = Split(FileList, "#")
j = UBound(Files) + 1
Application.ScreenUpdating = False
For i = 0 To j - 1
Set myDoc = Documents.Open(PathToUse & Files(i))
Me.lblNowProcessing.Visible = True
Me.lblNowProcessing.Caption = "Now processing " & Files(i)
Me.Repaint
'Call procedure to containing the processing code
ProcessDocument myDoc
With myDoc
.Saved = False
.Save
.Close
End With
Logstr = Logstr & Files(i) & " processed at " & Now & vbCr
Next i
i = i + 1
Application.ScreenUpdating = True
Me.lblFolder.Caption = "Completed processing files in " & PathToUse & "."
Me.lblNowProcessing.Visible = False
Me.cmdGo.Visible = False
Me.cmdCancel.Caption = "Exit"
Set Log = Documents.Add
Log.Range = "The following documents were processed from the folder " &
PathToUse & " at the times indicated:" & vbCr & Logstr
Log.SaveAs PathToUse & "Log.doc"
End Sub
Private Sub UserForm_Initialize()
Me.lblNowProcessing.Visible = False
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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