Option Explicit
Private CertNo As String
Private oHeader As Range
Private oHead As
HeaderFooter
Private oField As
Field
Private sName, sPath
As String
Private oVars As Variables
Private
bProtected As Boolean
Const sFormat As
String = "000#"
Const
sText As String = "Certificate No. "
Const
sPassword As String = ""
'The password to edit the form
Sub AutoNew() 'Add a
number when a new document is created
'If the document is a protected form, unprotect it
If ActiveDocument.ProtectionType <>
wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect _
Password:=sPassword
End If
sPath = Options.DefaultFilePath(wdStartupPath) & "\Settings.ini"
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber")
If CertNo = "" Then
'The settings file entry does not exist
CertNo = 1 'So set the start
number
Else 'The settings
file entry does exist so increment it by 1
CertNo = CertNo + 1
End If
'Define two document variables of the same initial
value
oVars("varCertNo").Value = CertNo
oVars("varSaveNo").Value = CertNo
'The number is to be placed in the header on the
first page
'so establish which type of header that is.
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
'Setup the format of the header.
'Note that this will remove any existing header content
'So ensure you include anything else required in the header
'Here we have some fixed text, and assorted formatting that should
'be self evident.
With oHeader
.Text = sText
.Font.Name = "Times New Roman"
.Font.Bold = True
.Font.Italic = False
.Font.Size = "16"
.ParagraphFormat.Alignment = wdAlignParagraphRight
.Collapse wdCollapseEnd
'Insert a docvariable
field to display the formatted number
.Fields.Add oHeader, wdFieldDocVariable, _
"""varCertNo"" \# " & sFormat,
False
.Fields.Update
End With
'Save the number in the INI file
System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber") = CertNo
'If the document was unprotected initially,
'
reprotect the
document for forms.
If bProtected = True
Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields,
NoReset:=True,
_
Password:=sPassword
End If
End Sub
Sub SaveCertificateAs()
sPath = Options.DefaultFilePath(wdStartupPath) & "\Settings.ini"
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber")
If Not
ActiveDocument.Path = "" Then
'The document has
previously been saved, so resave
ActiveDocument.Save
Else 'The document
has not been previously saved, so save it
'In the current document folder with a name including
the number
With
Dialogs(wdDialogFileSaveAs)
.Name = "Certificate " _
&
Format(oVars("varSaveNo"), _
sFormat)
.Show
End With
End If
'Close the document
ActiveDocument.Close
End Sub
Sub AutoClose() 'Recycles number if the
document was unsaved.'
sPath = Options.DefaultFilePath(wdStartupPath) & "\Settings.ini"
Set oVars = ActiveDocument.Variables
CertNo = System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber")
'Check if the document is unsaved
If ActiveDocument.Name Like "Document#*"
Then
'Offer the user the
opportunity to save
If MsgBox("This
Certificate has not been saved." & vbCr & _
"Do you want to save before
closing?", _
vbYesNo, "MacroSettings") =
vbYes Then
With Dialogs(wdDialogFileSaveAs)
.Name = "Certificate " _
& Format(oVars("varSaveNo"), _
sFormat)
.Show
End With
Else ' Close the document
and recycle the number
If CertNo = oVars("varCertNo") Then
MsgBox "The current number " & _
"will be recycled.", vbOKCancel, _
"Recycle"
System.PrivateProfileString(sPath, _
"MacroSettings", _
"CertificateNumber") = CertNo - 1
End If
End If
ActiveDocument.Saved = True
End If
End Sub
Sub ResetStartNo()
'Reset the number shown in the current document
and record
'it as the last used number
sPath = Options.DefaultFilePath(wdStartupPath) & "\Settings.ini"
Set oVars = ActiveDocument.Variables
'Get the current number from the settings file
CertNo = System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber")
'Get the user to input the new number
CertNo = InputBox("Reset certificate number?", _
"Reset", CertNo)
'Record the input number as a docvariable
oVars("varSaveNo").Value = CertNo
'Save the input number
System.PrivateProfileString(sPath, _
"MacroSettings", "CertificateNumber") = CertNo
'Set the new number as the displayed variable
content
oVars("varCertNo").Value = CertNo
'Update the header
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