auto number a word document each time it is opened

R

rachgg4

Hi there,

I would like to know how to have a number update automatically each time the
document is opened. I have tried all the avenues I know and still have had no
luck.
I am using word 2003 and have no experience with macros, however I am keen
to learn. I am struggling to get the below advice to previous users "
http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm. "
to work for me. It keeps coming up with error messages after I have created
the macro, the main one being "Compile Error: Expected Function or variable"
for the "Order =" and then will not attach it to my document.
I know many others have asked how to do this, I'm sorry if this is sounding
repetitive.
Many thanks in advance to anyone able to help me.

Kind regards
 
D

Doug Robbins - Word MVP

The easiest way for you to do that is go to the File>Properties dialog and
then go to the Custom tab and in the Name: field, type DocNumber and in the
Value: field, type the starting number that you wish to use e.g. 1, then
click on Add, then click on OK.

Then in your document, and in the location where you want the number to
appear, go to the Insert>Field dialog and scroll down through the list of
fields to the DocProperty field and then in the list of properties, select
the DocNumber item from the list. When you click OK, the starting number
that you entered will appear in the document.

To get this number to be updated each time the document is opened, create
the following macro

Sub AutoOpen()
On Error GoTo EndThis 'Exits the routine if the Custom Document Property
DocNumber has not be added to the document
With ActiveDocument
.CustomDocumentProperties("DocNumber") =
..CustomDocumentProperties("DocNumber") + 1
.Range.Fields.Update
End With

EndThis:

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, originally posted via msnews.microsoft.com
 
R

rachgg4

Once again, I am stuck in Macro world which is quickly consuming me :) It is
though, quite addictive and I am determined to win the fight I'm having with
my PC.
I work for a company which uses network servers so the document needs to be
accessed by different people but not necessarily at the same time. We operate
Windows XP Word 2003.
I am trying to create a document that will open with a sequential number,
but if the document is not used or opened accidentally then the number it has
generated needs to be recycled for auditing purposes. I seem to be able to
get the number to sequentially generate but the recycling of the number has
got me absolutely beat. I can’t for the life of me understand how to do it or
what I am doing wrong.
The other people who would be accessing the document are not very computer
literate and therefore I have tried to get the macro to AutoOpen so the
number is generated for them.
I have tried using code that MVP's have provided other users of the forum
with, but when I try incorporating all different macros I just make a big
mess.
I initially tried with an INI file but that got confusing too.
This is what I have so far ...

Sub AutoNew()
Dim Order As String
Dim sView As String

Order =
System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "MacroSettings",
"CertificateNumber", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "MacroSettings", "Order")
= Order

sView = ActiveWindow.View 'store the current view
With ActiveWindow
.View = wdPrintView 'set the document view to print layout
.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection
.WholeStory 'select any existing header text
.Delete Unit:=wdCharacter, Count:=1 'and delete it
.Font.Name = "Arial" 'Set the font characteristics
.Font.Bold = True 'of the inserted header
.Font.Italic = False 'if required
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight

'Insert the new header
.TypeText Text:="Certificate No: " & Format(Order, "200#")
End With
.View.SeekView = wdSeekMainDocument 'Close the header view
.View = sView 'and restore the initial view

Order =
System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "MacroSettings", "Order")


ActiveDocument.SaveAs
FileName:="S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems Isolations\Drier 2
System Isolations" & Format(Order, "200#")

End Sub


Sub AutoOpen()
Dim Order As String
Dim sView As String

Order =
System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "MacroSettings", "Order")

'Create one and set the number to 1, otherwise increment the number

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "InvoiceNumber", "Order")
= Order
sView = ActiveWindow.View 'store the current view
With ActiveWindow
.View = wdPrintView 'set the document view to print layout
.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection
.WholeStory 'select any existing header text
.Delete Unit:=wdCharacter, Count:=1 'and delete it
.Font.Name = "Arial" 'Set the font characteristics
.Font.Bold = True 'of the inserted header
.Font.Italic = False 'if required
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight

'Insert the new header
.TypeText Text:="Certificate No: " & Format(Order, "200#")
End With
.View.SeekView = wdSeekMainDocument 'Close the header view
.View = sView 'and restore the initial view

Order =
System.PrivateProfileString("S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations Sequence.Txt", "MacroSettings", "Order")


ActiveDocument.SaveAs
FileName:="S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems Isolations\Drier 2
System Isolations" & Format(Order, "200#")
End With
End Sub
'
' AutoOpen Macro
' Macro created 23/01/2010 by Rachel Watson
'

Sub AutoClose() 'Recycles number if document unsaved.'
If ActiveDocument.Name Like "S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems
Isolations\Drier 2 System Isolations*" Then
If MsgBox("This Certificate has not been saved. Do you want to save
before closing?", vbYesNo, "MacroSettings") = vbYes Then
Application.Dialogs(wdDialogFileSaveAs).Show
Else
If MsgBox("The current number will be recycled.", vbOKCancel,
"Recycle") = vbOK Then
SettingsFile = Options.DefaultFilePath(wdStartupPath) &
"\Settings.ini"
End If
ActiveDocument.Saved = True
ActiveDocument.AttachedTemplate.Saved = True
End If
End If

End Sub
'
' AutoClose Macro
' Macro created 23/01/2010 by Rachel Watson

If there is anyway at all you are able to give me any help or advice it
would be most appreciated.

Many thanks in advance,

rachgg4
 
G

Graham Mayor

You need to save the 'document' as a template and create new documents from
it, using an autonew macro to apply the number.

If your macro writes the next number to the ini file (which needs to be in a
location anyone using the template can access) then if the document is
closed without saving, you need an autoclose macro to decrement the stored
number to enable the number to be re-used. It is not possible for the macro
to second guess whether the user intended to keep a saved number or not. As
someone will inevitably screw it up, you need a macro to reset the start
number.

Create a new module in the template -
http://www.gmayor.com/installing_macro.htm and try the following, which
seems to do what your macros attempted to do.

Option Explicit
Private CertNo As String
Private oHeader As Range
Private sName As String
Const sPath = "S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems Isolations\"
Sub AutoNew()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If CertNo = "" Then
CertNo = 1
Else
CertNo = CertNo + 1
End If
If ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Exists Then
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
End If
With oHeader
.Text = "Certificate No: " & Format(CertNo, "200#")
.Font.Bold = True 'of the inserted header
.Font.Italic = False 'if required
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo
End Sub
Sub SaveCertificateAs()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Not ActiveDocument.Path = "" Then
ActiveDocument.Save
Else
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(CertNo, "200#")
.Show
End With
End If
ActiveDocument.Close
End Sub

Sub AutoClose() 'Recycles number if document unsaved.'
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Len(ActiveDocument.Path) = 0 Then
If MsgBox("This Certificate has not been saved." & vbCr & _
"Do you want to save before closing?", _
vbYesNo, "MacroSettings") = vbYes Then
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(CertNo, "200#")
.Show
End With
Else
MsgBox "The current number " & _
"will be recycled.", vbOKCancel, _
"Recycle"
End If
ActiveDocument.Saved = True
End If
End Sub

Sub ResetStartNo()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
CertNo = InputBox("Reset next certificate number?", "Reset", CertNo)
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo - 1
End Sub
 
R

rachgg4

I am so grateful for all the help.
This is absolutely brilliant!!
Only one thing that won't work properly is the recycling of the number. It
will ask if I want to save changes and I choose 'no' then asks 'Do I want to
recycle the number? (e.g 2001) and I say 'yes' but when the doc template is
opened for the next document to be generated, it has the next number as
'2002' not '2001'. Have I missed something yet again?
Sorry if I am not quite getting it.
Thanks in advance
rachgg4
 
G

Graham Mayor

Oops! My fault. The following version should fix the renumbering issues. Do
note however that when inserting the number as plain text (as in this
instance) you cannot change the number of the current document. (see later)


Option Explicit
Private CertNo As String
Private oHeader As Range
Private sName As String
Private oVars As Variables
Const sPath = "S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems Isolations\"
Sub AutoNew()
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If CertNo = "" Then
CertNo = 1
Else
CertNo = CertNo + 1
End If
oVars("varCertNo").Value = CertNo
If ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Exists Then
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
End If
With oHeader
.Text = "Certificate No: " & Format(CertNo, "200#")
.Font.Bold = True 'of the inserted header
.Font.Italic = False 'if required
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo
End Sub
Sub SaveCertificateAs()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Not ActiveDocument.Path = "" Then
ActiveDocument.Save
Else
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(CertNo, "200#")
.Show
End With
End If
ActiveDocument.Close
End Sub

Sub AutoClose() 'Recycles number if document unsaved.'
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Len(ActiveDocument.Path) = 0 Then
If MsgBox("This Certificate has not been saved." & vbCr & _
"Do you want to save before closing?", _
vbYesNo, "MacroSettings") = vbYes Then
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(CertNo, "200#")
.Show
End With
Else
If CertNo = oVars("varCertNo") Then
MsgBox "The current number " & _
"will be recycled.", vbOKCancel, _
"Recycle"
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo - 1
End If
End If
ActiveDocument.Saved = True
End If
End Sub

Sub ResetStartNo()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
CertNo = InputBox("Reset next certificate number?", "Reset", CertNo)
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo - 1
End Sub

Having looked again at the macros, I felt that it might make more sense to
the user to insert the number with a docvariable field rather than as plain
text. This allows the current document to be updated when the number is
reset. It needed a few extra lines of code to ensure that documents
displayed and were saved with the right number, but the principles involved
are much the same. I am however puzzled why you format the numbers as 200#.
Why not simply set the start number as 2001? However I have left it as you
had it.

Option Explicit
Private CertNo As String
Private oHeader As Range
Private oHead As HeaderFooter
Private oField As Field
Dim oRng As Range
Private sName As String
Private oVars As Variables
Const sPath = "S:\HLZ1-Hamilton\Morrinsville\MORRINS\Systems Isolations\"
Sub AutoNew()
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If CertNo = "" Then
CertNo = 1
Else
CertNo = CertNo + 1
End If
oVars("varCertNo").Value = CertNo
oVars("varSaveNo").Value = CertNo
If ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Exists Then
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
End If
With oHeader
.Text = "Certificate No: "
.Font.Bold = True 'of the inserted header
.Font.Italic = False 'if required
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight
.Collapse wdCollapseEnd
.Fields.Add oHeader, wdFieldDocVariable, _
"""varCertNo"" \# ""200#""", False
.Fields.Update
End With
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo
End Sub
Sub SaveCertificateAs()
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Not ActiveDocument.Path = "" Then
ActiveDocument.Save
Else
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations 200" _
& ActiveDocument.Variables("varSaveNo")
.Show
End With
End If
ActiveDocument.Close
End Sub

Sub AutoClose() 'Recycles number if document unsaved.'
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")
If Len(ActiveDocument.Path) = 0 Then
If MsgBox("This Certificate has not been saved." & vbCr & _
"Do you want to save before closing?", _
vbYesNo, "MacroSettings") = vbYes Then
With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations 200" _
& oVars("VarSaveNo").Value
.Show
End With
Else
If CertNo = oVars("varCertNo") Then
MsgBox "The current number " & _
"will be recycled.", vbOKCancel, _
"Recycle"
System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo - 1
End If
End If
ActiveDocument.Saved = True
End If
End Sub

Sub ResetStartNo()
Set oVars = ActiveDocument.Variables

CertNo = System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber")

CertNo = InputBox("Reset next certificate number?", _
"Reset", CertNo)

oVars("varSaveNo").Value = CertNo

System.PrivateProfileString(sPath & _
"Drier 2 System Isolations Sequence.ini", _
"MacroSettings", "CertificateNumber") = CertNo

oVars("varCertNo").Value = CertNo

For Each oHead In ActiveDocument.Sections(1).Headers
If oHead.Exists Then
For Each oField In oHead.Range.Fields
oField.Update
Next oField
End If
Next oHead
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

I have just spotted another glitch :(

Change both instances of

With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(CertNo, "200#")
.Show
End With

to

With Dialogs(wdDialogFileSaveAs)
.Name = sPath & "Drier 2 System Isolations " _
& Format(ActiveDocument.Variables("varSaveNo"), "200#")
.Show
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

rachgg4

It worked!!!!! :) :)
Thank you so much.
I did have to 'tweak' the code so it didn't double up on the wording in the
header, but other than that, it was absolutely brilliant!
I couldn't have done this without your help and expertise. All the time and
effort you have provided is so appreciated and I'm sure other forum users
will find this such a help.
Many thanks,
rachgg4
 

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