Log File

J

JoeBo

I need some code that will add the filename and path as well as the current
date and time to a log file called word files.txt each time a word document
is opened or saved. Anyone Help

Regards

JoeBo
 
A

Anand.V.V.N

Hi,

Hope the sample code helps you.

Create a copy of the normal.dot nad then modify the normal.dot. You can
edit the macros, and modify the inbuilt macros for saving opening, and
closing file. I am not sure which are the macros.

Other way is to open the visual basic editor and in the open document, new
document, you can call a function called writelog, and write a function,
write the code there.

To get the path and name

ActiveDocument.Path gives you the document path
ActiveDocument.Name gives you the document name

open a text file in the path you want in append mode, and every time the
file is opened, closed or saved, you can call a function that opens the text
file in appened mode and usign this statement you can write the log.

in the function writelog use this code.

dim filnu
filenu=Freefile

open <path><filename> for append as filenu 'repalce the path and filename
with the opat and filename,

with activedocument
if len( activedocument.path) > 3 then
write filenu, .path &"\" & .name ' if it is not the root directory, eg
"c:\path1.
else
write filenu, .path & .name ' if it is the root directory, eg "c:"
endif

'Use format function to write the date and time
write filenu,format(now(),"dd,dddd,mmmm,yyyy") 'any other format also can be
used like "dd/mm/yy" or "d,dd,dddd,mmmm,yyyy", "mm-dd-yy" etc

write filenu,format(now(),"hh:ss:mm")

close filenu

Hope you found this helpful. Is this what you wanted?

Anand
 
J

JoeBo

Hello Anand

Thanks for your response. I have attempted to copy it into a module but the
code complains of a compile error when the "write line" statement is
inserted, that a # sign is missing. I wrapped # signs around the statement as
follows:

Write #filenu#, .Path & "\" & .Name

but when the code is compiled it complains that:

Type declaration character does not match declared data type

Any suggestions.

Regards
 
A

Anand.V.V.N

Hi Joe,

Write #filenu#, .Path & "\" & .Name

use write #filenu, .path & "\" & .name

Or try this code it should be better

dim filestring as string

with activedocument

if len(.paht)> 3 then

filestring= .path & "\" & . name

else
filestring=.path & .name
endif
end with

open <path> file.txt for append as #filenu

write #filenu,filestring
write #filenu,format(now(),"dd,dddd,mmmm,yyyy")
write #filenu,format(now(),"hh:mm:ss")
close #filnu

Hope this code works and you find it helpful.

Anand
 
J

JoeBo

Hello Anand

Thank you for your patience, I have loaded your new code but I may have
misinterpreted the following line:

open <path> file.txt for append as #filenu

My file is called wordlog.txt and is in the route directory "C:\" I have
replaced the line above with:

Open "C:\ excellog.txt" For Append As #filenu

But I receive the error message:

"Bad file name or Number"

Would you please post the correct syntax.

Many Thanks

Regards
JoeBo
 
A

Anand.V.V.N

Hi Joe,
I think you have a extra space before the file name i.e. an extra space
before teh file name remove the extra space and try it should solve the
problem.

i.e. instead of "C:\ excellog.txt" use "C:\excellog.txt"

Hope this solves the problem.

Anand
 
J

JoeBo

Hello Anand

Thanks again but I am missing something, but I don't know what, my path and
filename when posted into: Start run finds and opens the file, but VBA still
complains of a bad files name> I have posted the full code below. hope you
can help.


Public Function LogFile()


Dim filestring As String

With ActiveDocument

If Len(.Path) > 3 Then

filestring = .Path & "\" & .Name

Else
filestring = .Path & .Name
End If
End With

Open "c:\ExcelLog.txt" For Append As #filenu
'Open "C:" & "excellog.txt" For Append As #filenu

Write #filenu, filestring
Write #filenu, Format(Now(), "dd,dddd,mmmm,yyyy")
Write #filenu, Format(Now(), "hh:mm:ss")
Close #filnu

End Function

Regards
JoeBo
 
A

Anand.V.V.N

Hi Jeo,

I am very sorry I missed out one thing

Have this line after this line

Dim filestring As String

Dim filenu as integer

And before the open file statement assing filenu to freefile

filenu=FreeFile


With these two addition I am sure the code would work. And I very sorry to
miss that out.


Anand
 
J

JoeBo

Hi Anand

Thanks for your time the code now works as you said.

Many Thanks

Regards
JoeBo
 

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