Count number of times a file is opened

K

Krakmup

G'Day

I want to count the number of times a file is inserted into documents. I am
trying to start with a number inside a table cell, read the cell, put the
number from the cell into a variable, add 1 to the variable, save the
variable into the table cell, then save the file as the same name.

I am having trouble reading the cell value, and placing it into the
variable, so I can add 1 to it.

Thanks
Krakmup
 
J

Jezebel

It would be simpler to put the value into a custom documentproperty, or into
a document variable.

Dim pCount as long
Const pVarName as string = "OpenCount"

on error resume next
pCount = ActiveDocument.Variables(pVarName)
on error goto 0

pCount = pCount + 1
ActiveDocument.Variables(pVarName) = pCount
 
K

Krakmup

G'Day
If I add a custom document property to the DocumentProperties collection
that’s linked to a variable, don't I have to save the document to see the
change to the DocumentProperty object.
Krakmup
 
J

Jezebel

no



Krakmup said:
G'Day
If I add a custom document property to the DocumentProperties collection
that's linked to a variable, don't I have to save the document to see the
change to the DocumentProperty object.
Krakmup
 
K

Krakmup

G'Day
I used 'document variable' and was able to increment it through the .ini
file, how would I increment a custom document property with "pcount = pcount
+ 1", inserting the new pcount value into the already created custom document
property?

Thanks for the tip.
Krakmup
 
J

Jezebel

Not sure that I still understand whatr you're doing. Where does the INI file
come in? The whole point of using Doc Variables or Doc Properties is that
the value is saved with the template or document that contains it.

To work with a document property --

pCount = ActiveDocument.CustomDocumentProperties("OpenCount")
pCount = pCount + 1
ActiveDocument.CustomDocumentProperties("OpenCount") = pCount


You might want to add some error-handling: the code will fail if there
property is not defined or has been redefined to the wrong data type.
 
K

Krakmup

G'Day
Actually, the help file, under document variables, directed me to code,
similar to this, which generates the .ini file:

Sub GetSystemFileInfo()
Dim intDocNum As Integer
Dim intAge As Integer

intDocNum = System.PrivateProfileString( _
filename:="C:\My Documents\Macro.ini", _
Section:="DocTracker", Key:="DocNum")

intAge = intDocNum + 1
System.PrivateProfileString( _
filename:="C:\My Documents\Macro.ini", _
Section:="DocTracker", Key:="DocNum") = intAge

MsgBox "AEM Safety Manual Use = " & intAge
End Sub

It works well, but I am also going to try CustomDocumentProperties method.

Krakmup
 

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