View Single Post
 
Old 06-10-2019, 07:18 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For your situation, it would probably be best to create a Custom Document Property named 'InvNum' in the template, then use code like:
Code:
Private Sub Document_New()
Application.ScreenUpdating = False
Dim InvNum As String
With ThisDocument
  InvNum = .CustomDocumentProperties("InvNum") + 1
  .CustomDocumentProperties("InvNum") = InvNum
  '.Save
End With
With ActiveDocument
  'Update the value stored in the document property
  .CustomDocumentProperties("InvNum") = InvNum
  'Update the fields in the document
  .Fields.Update
End With
Application.ScreenUpdating = True
End Sub
Do note that the number will increment every time a new document is created from the template. If someone does that, then doesn't save the new document, you'll invariably end up with gaps in the numbers in the saved documents. You can, of course, manually edit the 'InvNum' Custom Document Property in the template, but it's better to avoid the situation in the first place. More complex code could be used to ensure the numbers are only updated when the template is closed, but you then run the risk of the same number being allocated twice if two or more documents are being created at the same time.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote