Home |
Search |
Today's Posts |
#10
![]()
Posted to microsoft.public.word.newusers
|
|||
|
|||
![]()
You are welcome
![]() -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org "rachgg4" wrote in message ... 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 "Graham Mayor" wrote: I have now revised the code on my web page http://www.gmayor.com/automatic_numbering_documents.htm to reflect the code shown here (albeit without your personal settings). The code is fully annotated so that you can see how it works. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org "Graham Mayor" wrote in message ... 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 Word MVP web site http://word.mvps.org "Graham Mayor" wrote in message ... 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(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.' 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(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: " .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 Word MVP web site http://word.mvps.org "rachgg4" wrote in message ... 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 |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How to make a template number itself each time opened (invoicing) | Microsoft Word Help | |||
number sequence field updated each time the doc. is opened | Microsoft Word Help | |||
Document numbering is not resetting at 1 each time Word is opened | Microsoft Word Help | |||
how do I automatically re-number my doc each time it's opened? | Microsoft Word Help | |||
How do you have Word Auto-number your forms every time you open o. | Microsoft Word Help |