Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Macro for adding a sequential number to a template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
stalley1995 - 25 Jan 2006 00:20 GMT
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.
Greg Maxey - 25 Jan 2006 00:59 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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
[quoted text clipped - 3 lines]
>
> Thanks.
Jay Freedman - 25 Jan 2006 01:55 GMT
>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
[quoted text clipped - 3 lines]
>
>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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
stalley1995 - 26 Jan 2006 06:43 GMT
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-
Graham Mayor - 26 Jan 2006 06:53 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> 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-
stalley1995 - 27 Jan 2006 19:58 GMT
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-
Doug Robbins - Word MVP - 27 Jan 2006 20:53 GMT
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

Signature

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

> 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
[quoted text clipped - 5 lines]
> Thanks.
> -Scott-
stalley1995 - 28 Jan 2006 06:55 GMT
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-
stalley1995 - 29 Jan 2006 21:56 GMT
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-
Doug Robbins - Word MVP - 30 Jan 2006 05:03 GMT
Change

Dim SettingsFile As String, Order as Long

to

Dim SettingsFile As String, Order as Variant

Signature

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

> Doug and group;
> I ran the new macro and now I am getting a new message:
[quoted text clipped - 16 lines]
> Thanks again.
> -Scott-
Graham Mayor - 30 Jan 2006 08:04 GMT
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.

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Doug and group;
> I ran the new macro and now I am getting a new message:
[quoted text clipped - 16 lines]
> Thanks again.
> -Scott-
stalley1995 - 30 Jan 2006 17:01 GMT
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-
Jay Freedman - 30 Jan 2006 18:41 GMT
> I've tried changing the Order to Variant and that didn't solve the
> problem.
[quoted text clipped - 9 lines]
> 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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Doug Robbins - Word MVP - 30 Jan 2006 18:46 GMT
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.

Signature

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

> I've tried changing the Order to Variant and that didn't solve the
> problem.
[quoted text clipped - 9 lines]
> Again, thanks so much for everyones help.
> -Scott-
stalley1995 - 31 Jan 2006 17:14 GMT
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-

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.