Hi Lee
An AddIn is a template that is generally loaded Automatically when Word starts. You
generally store the AddIn in the folder specified in Tools>Options>File Locations>Startup.
Here's the code you'll need to construct an addin to count the usage of *any* template
used to create a new document while the AddIn is loaded. To use this code you need to
create a new template "count template usage.dot". To this template from within the VBA
IDE (editor) create a new Module. To this new module add the following code (cut and
paste it):
'<----------------------Start of Module Code---------------------------->
Option Explicit
Public gccTemplateUsageCounter As clsCounter
Public Sub AutoExec()
Const cCounterFileFullPath As String = "c:\template usage count.cnt"
' Instantiate class to perform template usage counting
Set gccTemplateUsageCounter = New clsCounter
' Hook up event handler to that we can catch application level events
Set gccTemplateUsageCounter.appWord = Word.Application
' Log the existing counter file (if any)
gccTemplateUsageCounter.Initialise cCounterFileFullPath
End Sub
'<----------------------End of Module Code---------------------------->
Now add a new Class Module to your project, this class module *must* be named
"clsCounter". To this new class module add the following code (cut and paste it):
'<--------------Start of Class Module clsCounter Code--------------------->
Option Explicit
Private Const mcInitialSize As Long = 50
Private Const mcIncrementSize As Long = 10
Private Type UsageCounter
Template As String
Count As Long
End Type
Public WithEvents appWord As Word.Application
Private maucCounters() As UsageCounter
Private mlngCountersMax As Long
Private mstrCounterFile As String
Public Sub Initialise(ByVal strCounterFile As String)
Dim lngIndex As Long
Dim strData As String
Dim astrData() As String
' Set array to it's initial sizing
ReDim maucCounters(0 To mcInitialSize - 1) As UsageCounter
mlngCountersMax = -1
' Save the counter file name for when we need to update it
mstrCounterFile = strCounterFile
' Read the counter file and load the contents into the array
strData = ReadFile(strCounterFile)
' Parse the counter file a line at a time
astrData = Split(strData, vbCrLf)
For lngIndex = 0 To UBound(astrData)
' Now parse out the data: Path, Template, Counter
AddExistingCounter astrData(lngIndex)
Next
' Make sure the array if not too flabby
If mcInitialSize - mlngCountersMax > mcIncrementSize - 1 Then
ReDim Preserve maucCounters(0 To mlngCountersMax + mcIncrementSize)
End If
End Sub
Private Sub appWord_NewDocument(ByVal Doc As Document)
' Count the usage of the current template
CountCurrentTemplate Doc.AttachedTemplate
End Sub
Private Function ReadFile(ByVal strInputFile As String) As String
Dim strInput As String
Dim hFile As Long
' Next available file number
hFile = FreeFile
' Open and read the entire file
Open strInputFile For Binary Access Read Shared As hFile
ReadFile = Input(LOF(hFile), hFile)
' All done - so close the file
Close hFile
End Function
Private Sub WriteFile(ByRef rstrData As String)
Dim hFile As Long
' Next available file number
hFile = FreeFile
' Open and write the entire file
Open mstrCounterFile For Binary Access Write Shared As hFile
Put hFile, , rstrData
' All done - so close the file
Close hFile
End Sub
Private Sub AddExistingCounter(ByVal strData As String)
Dim astrDatum() As String
' The array must be large enough to hold the data
MakeSpaceInArray
' Parse out the data (FullPath, Counter) and add it to the array
astrDatum = Split(strData, ",")
With maucCounters(mlngCountersMax)
.Template = astrDatum(0)
.Count = CLng(astrDatum(1))
End With
End Sub
Private Sub CountCurrentTemplate(ByVal tplTemplate As Word.Template)
Dim lngIndex As Long
Dim boolFound As Boolean
' Try to locate and use an existing counter before creating a new one
If mlngCountersMax >= 0 Then
For lngIndex = 0 To mlngCountersMax
With maucCounters(lngIndex)
If StrComp(.Template, tplTemplate.FullName, vbTextCompare) = 0 Then
.Count = .Count + 1
boolFound = True
End If
End With
Next
End If
' Add a new counter
If boolFound = False Then
' The array must be large enough to hold the data
MakeSpaceInArray
With maucCounters(mlngCountersMax)
.Template = tplTemplate.FullName
.Count = .Count + 1
End With
End If
' Save the template usage count information
SaveCounterInformation
End Sub
Private Sub MakeSpaceInArray()
' Make sure the array is large enough to hold the next piece of data
' if not then increase its size by the specified increment size
mlngCountersMax = mlngCountersMax + 1
If mlngCountersMax > UBound(maucCounters) Then
ReDim Preserve _
maucCounters(0 To UBound(maucCounters) + mcIncrementSize) As UsageCounter
End If
End Sub
Private Sub SaveCounterInformation()
Dim lngIndex As Long
Dim strData As String
' Concatenate all data into one string, separate the Template
' and Counter using a comma and each line using vbCr
If mlngCountersMax >= 0 Then
For lngIndex = 0 To mlngCountersMax
With maucCounters(lngIndex)
If lngIndex > 0 Then
strData = strData & (vbCrLf & .Template & "," & CStr(.Count))
Else
strData = (.Template & "," & CStr(.Count))
End If
End With
Next
End If
' Write the data to the counter file
WriteFile strData
End Sub
'<----------------------End of Class Module Code---------------------------->
If you get really stuck email me and I'll email you the template that contains the above
code. But it would be a good idea to become familiar with Word/VBA and the VBA
environment yourself. My email address is:
nospaminnerword at xtra dot co dot nz
remove the "nospam" and replace dot with . and at with @. Also because I run two spam
filters (one at my ISP and one on my PC) you message will NOT get through unless you mark
the subject line: "microsoft.public.word.vba.general - AddIn"
HTH + Cheers - Peter
Thank you for replying to my post.
Every time a new document is created based on that
template. Within most of my templates I have a macro
called AutoNew but I sort of understand what you mean by
storing the coding into an add-in... The thing is I am not
sure where/how to even start coding this. It was my
thought to have some code that would add the number of
times each template was accessed and to save it to a
logfile BUT how would it work if I only had one log file??
Kinda confused...
Lee
HTH + Cheers - Peter