View Single Post
 
Old 06-23-2011, 10:35 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

Hi mslearner,

That approach won't give you an automatically-incrementing work order number.

The following macro (which I wrote for invoice generation) inserts and increments a number from a text (ini) file. It should serve your needs equally well.
Code:
Private Sub Document_New()
Application.ScreenUpdating = False
Dim InvoiceFile As String, InvNum As String
'Save ini file in the Word startup folder.
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, by using the following line, the Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
'If there is no InvoiceNumber reference in the ini file
'Create one and set the number to 1, otherwise increment the number
If InvNum = "" Then
  InvNum = 1
Else
  InvNum = InvNum + 1
End If
System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = InvNum
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
To use the code, you need to open your work order template and add the macro to its 'ThisDocument' code module.

Then, go to File|Info|Properties|Custom and add a document property named 'InvNum'.

Next, in the body of the work order template, position the insertion point where you want the work order number to appear and press Ctrl-F9 to create a pair of field braces (ie '{}') and type 'DOCPROPERTY InvNum' between the field braces, thus '{DOCPROPERTY InvNum}'. If you want to force the work order # to display leading 0s (eg 01234), add '\# 0000' to the field code, thus: '{DOCPROPERTY InvNum \# 0000}'.

Finally, save the template as a Word document Template (ie with a '.dot' (Word 2004 & earlier) or '.dotm' (Word 2007 & later) extension, via File|Save As), named 'Invoice' (or another suitable name).

Now, whenever you want to create a new work order, simply go to File|New and select 'work order' (or your preferred name). You'll get an work order with the next available number. The last-used number is held in a file named 'Invoice.ini' in your Word Startup folder. In case you need it, there's a commented-out line to store the 'Invoice.ini' in your Word Workgroup folder instead, so that others in your workgroup can access it also.

If you need to set the initial work order #, or reset it, you can do so manually, or use the following macro:
Code:
Sub ResetOrderNumber()
Dim InvoiceFile As String, InvNum As String
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, if using a Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
InvNum = Trim(InputBox("What is the last valid Work Order Number?" & vbCrLf & _
  "The current number is: " & InvNum))
If IsNumeric(InvNum) Then
  System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = CInt(InvNum)
  End
Else
  If InvNum = "" Then End
End If
MsgBox "Renumbering error, please try again."
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote