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
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>