View Single Post
 
Old 03-23-2013, 01:55 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

If you add the following code and a Custom Document Property named "RefNum" to the file template's 'thisDocument' code module, every new file you create from the template will be allocated an incrementing number:
Code:
Sub Document_New()
Dim StrDefPath As String, StrTmpPath As String
Dim RefFile As String, RefNum As String, Rng As Range
'Indicate the default save path for files created from this template,
' including the final '\'.
StrTmpPath = "Filepath for documents based on this template"
StrDefPath = Options.DefaultFilePath(wdDocumentsPath)
Options.DefaultFilePath(wdDocumentsPath) = StrTmpPath
RefFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\RefNumber.txt"
If Dir(RefFile) = "" Then
  'If the Reference Number File file in the Word Workgroup folder, create it
  RefNum = 0
  Open RefFile For Output As #1
  Write #1, RefNum
  Close #1
End If
'Read the Reference Number File file in the Word Workgroup folder
Open RefFile For Input As #1
Input #1, RefNum
Close #1
' Increment the Reference Number
RefNum = RefNum + 1
DoEvents
'Update the Reference Number File file in the Word Workgroup folder
Open RefFile For Output As #1
Write #1, RefNum
Close #1
'Update the Reference Number in the document and prompt to save
With ActiveDocument
  .CustomDocumentProperties("RefNum") = RefNum
  Application.ScreenUpdating = False
  For Each Rng In ActiveDocument.StoryRanges
    Rng.Fields.Update
  Next
  Application.ScreenUpdating = True
  With .Dialogs(wdDialogFileSaveAs)
    .Name = StrTmpPath & RefNum
    .Show
  End With
End With
Options.DefaultFilePath(wdDocumentsPath) = StrDefPath
End Sub
 
Sub ResetReferenceNumber()
Dim RefFile As String, RefNum As String
'Re-set the Reference Number File file in the Word Workgroup folder
RefFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\RefNumber.txt"
Open RefFile For Input As #1
Input #1, RefNum
Close #1
RefNum = Trim(InputBox("What is the last valid Reference Number?" & vbCrLf & _
  "The current number is: " & RefNum))
If IsNumeric(RefNum) Then
  If CInt(RefNum) = RefNum Then
    Open RefFile For Output As #1
    Write #1, CInt(RefNum)
    Close #1
    Exit Sub
  End If
End If
MsgBox "Re-numbering error, please try again."
End Sub
The reference numbering is controlled by a text file stored in the workgroup templates folder, so that all members of the workgroup can use the same numbering sequence.

You could omit the Custom Document Property named "RefNum", but having it means the number remains with the document even if the filename is changed later on. If you want to give the file a default name that is more meaningful than just the reference number, you'll need to specify the rules for that.

The code also includes a StrTmpPath variable, for nominating the default save path for files created from the template.

I've also included some code to re-set the reference numbers.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote