View Single Post
  #5   Report Post  
Posted to microsoft.public.word.newusers
rachgg4 rachgg4 is offline
external usenet poster
 
Posts: 6
Default auto number a word document each time it is opened

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

"Graham Mayor" wrote:

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(wdHeaderFooterF irstPage).Exists Then
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Range
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).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




"rachgg4" wrote in message
...
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



"Doug Robbins - Word MVP" wrote:

The easiest way for you to do that is go to the FileProperties 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 InsertField 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