Inserting a counter in a Word Document

O

olleber

I have just started looking into Visual Basic in order to insert a counter into a document. The counter should increment each time the document is openned. I haven't done very well so far. Anyone care to help?
 
H

Helmut Weber

Hi,
have a look at this:
Sub autoopen()
Dim lCnt As Long
Dim oDcm As Document
Set oDcm = ActiveDocument

With oDcm
' restrict numbering to "C:\Test\Counter.doc"
If .FullName = "C:\Test\Counter.doc" Then
On Error Resume Next
' property "MyNumber" may not exist
lCnt = .CustomDocumentProperties("MyNumber").Value
' if so then create property "MyNumber"
If Err.Number = 5 Then
lCnt = 0
.CustomDocumentProperties.Add _
Name:="MyNumber", _
LinkToContent:=False, _
Value:=0, _
Type:=msoPropertyTypeNumber
End If
On Error GoTo -1 ' clear error
lCnt = lCnt + 1
.CustomDocumentProperties("MyNumber").Value = lCnt
.Saved = False
' Increase counter even if doc wasn't changed
.Save
End If
' for testing only
MsgBox .CustomDocumentProperties("MyNumber").Value
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
M

Martin

Hi ollebar,
try something like this

Sub autoopen()
Dim myname As String
Static mynum As Long
mynum = mynum + 1
myname = Application.UserName
MsgBox "Hi " & myname & Chr(10) & "Document opened " &
mynum & " times."
End Sub

HTH
Martin
-----Original Message-----
I have just started looking into Visual Basic in order to
insert a counter into a document. The counter should
increment each time the document is openned. I haven't
done very well so far. Anyone care to help?
 
P

Peter Hewett

Hi Martin

Static variables don't get saved with the document, so the value will only persist will
the code is in memory.

Cheers - Peter

Hi ollebar,
try something like this

Sub autoopen()
Dim myname As String
Static mynum As Long
mynum = mynum + 1
myname = Application.UserName
MsgBox "Hi " & myname & Chr(10) & "Document opened " &
mynum & " times."
End Sub

HTH
Martin
insert a counter into a document. The counter should
increment each time the document is openned. I haven't
done very well so far. Anyone care to help?

HTH + Cheers - Peter
 
D

Dave Neve

Hi

Another question if I may.

Normally, I have to click on 'execute' or an icon in the tool bar to execute
code.

What surprised me here is that every time I open the document, your code
executes itself and the msg box comes up with the number displayed.

But at the beginning of the code , there is nothing special which I can see
to 'trigger' it.

So how does it 'trigger' please
 
D

Doug Robbins - Word MVP

It's the name of the macro - autoopen. It will run whenever the document is
opened.

Check out the Auto macros topic in the visual basic help file.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
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