Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2011, 10:35 PM
macropod's Avatar
macropod macropod is offline Sequential numbering across multiple docs Windows 7 64bit Sequential numbering across multiple docs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
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
  #2  
Old 06-24-2011, 01:41 PM
mslearner mslearner is offline Sequential numbering across multiple docs Windows XP Sequential numbering across multiple docs Office 2003
Novice
Sequential numbering across multiple docs
 
Join Date: Jun 2011
Posts: 2
mslearner is on a distinguished road
Default

Thanks so much, this worked!
Reply With Quote
  #3  
Old 06-07-2019, 04:15 AM
wolfgrrl wolfgrrl is offline Sequential numbering across multiple docs Windows 10 Sequential numbering across multiple docs Office 2010
Novice
 
Join Date: May 2019
Posts: 15
wolfgrrl is on a distinguished road
Default

I realize that this is an old thread but I've been searching hours for a way to do this on a template. I've tried your macro and it's not functioning for me.

I get a compile error on the first line: ambiguous name detected: Document_New. I'm using Word 2010 and am opening my template from within Word.

Can you please help? Thank you!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sequential numbering across multiple docs How do I do a Sequential Numbering? gburya Word VBA 26 07-04-2017 03:29 PM
Sequential numbering across multiple docs Sequential Page Numbering of Multiple Word Docs bobmard Word 8 08-24-2011 08:51 AM
Sequential numbering across multiple docs Editing multiple docs frankdh Word 2 11-02-2010 10:59 AM
Please help with Numbering a document with multiple letters. DJReality213 Office 1 01-15-2010 05:56 PM
Amend footer in multiple word docs? compact Word 2 02-24-2009 09:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft