Macro for adding a sequential number to a template

S

stalley1995

I have a Word template for work orders that I would like to be able to
have a sequential number generated and inserted when I need it and not
have it automatically inserted every time I open it. I've look in a
number of places but it seems something this simple is not discussed.
Any ideas or help would be greatly appreciated.

Thanks.
 
G

Greg Maxey

I suppose that you could use a DocVariable field in the document

{ DocVariable "StoredNum" \# "000#" }

Run the following macro when you want to generate a number:
Sub MySeqNumber()
Dim LastNum As Long
On Error GoTo Handler
LastNum = ActiveDocument.Variables("StoredNum").Value
ActiveDocument.Variables("StoredNum").Value = LastNum + 1
ActiveDocument.Fields.Update
Exit Sub
Handler:
ActiveDocument.Variables("StoredNum").Value = "0"
Resume
End Sub
Sub Reset()
ActiveDocument.Variables("StoredNum").Delete
End Sub
 
J

Jay Freedman

I have a Word template for work orders that I would like to be able to
have a sequential number generated and inserted when I need it and not
have it automatically inserted every time I open it. I've look in a
number of places but it seems something this simple is not discussed.
Any ideas or help would be greatly appreciated.

Thanks.

Use the macro in
http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm but, instead of
naming the macro AutoNew (which causes it to run every time you make a
new document based on the template), name it something else like
WorkOrderNumber. Then create a toolbar button or keyboard shortcut to
run the macro when you want it
(http://www.word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToToolbar.htm
or
http://www.word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToHotkey.htm).
You probably also want to remove the line near the end of the macro
that saves the document with the invoice number in the filename.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

stalley1995

I want to thank all of you for your replies. The suggestions worked
wonderfully and I really appreciated how quickly everyone responded.
Again, thank you very much!
-Scott-
 
G

Graham Mayor

Maybe a bit late now the other guys have been quick off the mark, but the
following example will store the number in the registry. If you want to see
how to have individual number sequences for a variety of documents see the
modifications to the registry section at
http://www.gmayor.com/save_numbered_versions.htm

Sub AddNoFromRegistry()
Dim iCount As Integer
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim sDefault As String
Dim sView As String
Dim sCount As String

Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next
rkeyWord = WSHShell.RegRead(RegKey & "AddNo")
If rkeyWord = "" Then
WSHShell.regwrite RegKey & "AddNo", 0
GoTo Start:
End If
sDefault = InputBox("Reset start number?" & vbCr & _
"Enter any character to restart the numbering", "Reset", "No")
If sDefault <> "No" Then
iCount = 1
Else
iCount = Val(rkeyWord) + 1
End If
WSHShell.regwrite RegKey & "AddNo", iCount

' here the code is to print in header the registry value
sView = ActiveWindow.View
ActiveWindow.View = wdPrintView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
sCount = Format(iCount, "000")
Selection.TypeText Text:=sCount
ActiveWindow.View.SeekView = wdSeekMainDocument
ActiveWindow.View = sView
End Sub



--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

stalley1995

Well I have managed to come up with another small problem. I decided
on using the AutoNew macro by Doug Robbins and it has worked great. I
then I moved the template to another system so two other people would
have access to it (shared folder) from their PCs but the macro looks to
the local C:\Settings.txt file (on their systems) for the sequential
number. I have tried to change the pathway but I seem to be unable to
change the location of the Settings.txt file in the macro. Any ideas?
Thanks.
-Scott-
 
D

Doug Robbins - Word MVP

I am not sure why you cannot change the location of the Settings.txt file.
The following may not be related, but I have recently become aware that it
the user does not have administrator access under Windows XP, they are not
able to write to the root directory of the C: drive. To overcome this
problem, the following modified code should be used

Sub AutoNew()

Dim SettingsFile As String, Order as Long

SettingsFile = Options.DefaultFilePath(wdDocumentsPath) & "\Settings.txt"

Order = System.PrivateProfileString(SettingsFile, _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString(SettingsFile, "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

stalley1995

The two users have admin rights to their individual systems
(runningWindows XP Pro) and have admin rights to log onto the system I
am using for the template. The template is on a system running Windows
2000 professional. I'll try your suggestion tomorrow and once again,
thank you very much for your time and help.

-Scott-
 
S

stalley1995

Doug and group;
I ran the new macro and now I am getting a new message:

run time error '13'
Type Mismatch

This is the line highlighted:
If Order = "" Then

Any suggestions?


Some additional background, I renamed the original macro from "AutoNew"
to "WorkOrderNumber" and removed the last line of the original macro in
order to control when I wanted the sequential number added to the
document. It worked beautifully until I placed it on a central Win
2000 PC and then I discovered the number would not be in sequence
depending who utilized the template.

Thanks again.
-Scott-
 
D

Doug Robbins - Word MVP

Change

Dim SettingsFile As String, Order as Long

to

Dim SettingsFile As String, Order as Variant

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

If you want the same number sequence for all users, the same settings.txt
must be at a location available to all users. If you want separate numbers
for each user then settings.txt must be set to a user location.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

stalley1995

I've tried changing the Order to Variant and that didn't solve the
problem.

The settings.txt file is located on the Win2000 system and available to
all (and both users have admin rights to the Win2000 box).
Unfortunately, the two XP systems which access the folder for the
templates insist on using their local C: drives and have set up their
own Settings.txt files and there in lies the problem. I've tried
different approaches to pointing to the folder where the template and
Settings.txt file resides on the Win2000 system but so far no luck.
Any other suggestions?
Again, thanks so much for everyones help.
-Scott-
 
J

Jay Freedman

stalley1995 said:
I've tried changing the Order to Variant and that didn't solve the
problem.

The settings.txt file is located on the Win2000 system and available
to all (and both users have admin rights to the Win2000 box).
Unfortunately, the two XP systems which access the folder for the
templates insist on using their local C: drives and have set up their
own Settings.txt files and there in lies the problem. I've tried
different approaches to pointing to the folder where the template and
Settings.txt file resides on the Win2000 system but so far no luck.
Any other suggestions?
Again, thanks so much for everyones help.
-Scott-

Hi Scott,

Reading through the thread, it looks like you have two separate problems.
Fixing either one alone won't help.

One problem is getting both XP systems to see the Settings.txt file on the
Win2000 system. To fix that, you need to edit the two lines in the macro
that say

System.PrivateProfileString("C:\Settings.Txt",

so the path points to the shared location on the Win2000 system instead of
the local C: drive. You haven't said whether the XP systems have a drive
mapped to the Win2000 system (so it might be "X:\somefolder\Settings.txt")
or whether they get access through a UNC address like
"\\server\share\Settings.txt", but either way should work.

The second problem is the error 13 "type mismatch". Declaring the variable
Order as a Variant should have fixed that.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Doug Robbins - Word MVP

Do you mean the "Run time error '13' Type Mismatch" problem, or some other
problem.

In the template(s), you must set the path to the Settings.txt file to a
folder to which all users have read/write access. It you want multiple
users to be able to obtain numbers from the one sequence, this path will NOT
be to their c: drive.



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

stalley1995

Gentlemen:
I used the UNC path to the Settings.Txt file and now everything is
working well. Thank you so much for all your help, I really, really
appreciate it.
-Scott-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top