Group:  Microsoft Word ยป microsoft.public.word.vba.beginners
Thread: Macro for adding a sequential number to a template

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 25.01.2006 00:20:59
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.

Re: Macro for adding a sequential number to a template
"Greg Maxey" <gmaxey[ at ]mvps.oSCARrOMEOgOLF> 25.01.2006 00:59:23
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



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


stalley1995 wrote:
[Quoted Text]
> 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.


Re: Macro for adding a sequential number to a template
Jay Freedman <jay.freedman[ at ]verizon.net> 25.01.2006 01:55:04
On 24 Jan 2006 16:20:59 -0800, "stalley1995" <stalley1995[ at ]yahoo.com>
wrote:

[Quoted Text]
>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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 26.01.2006 06:43:23
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-

Re: Macro for adding a sequential number to a template
"Graham Mayor" <gmayor[ at ]mvps.org> 26.01.2006 06:53:29
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

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

stalley1995 wrote:
[Quoted Text]
> 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-


Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 27.01.2006 19:58:17
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-

Re: Macro for adding a sequential number to a template
"Doug Robbins - Word MVP" <dkr[ at ]REMOVETHISmvps.org> 27.01.2006 20:53:40
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

"stalley1995" <stalley1995[ at ]yahoo.com> wrote in message
news:1138391897.511194.34940[ at ]o13g2000cwo.googlegroups.com...
[Quoted Text]
> 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-
>


Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 28.01.2006 06:55:42
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-

Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 29.01.2006 21:56:02
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-

Re: Macro for adding a sequential number to a template
"Doug Robbins - Word MVP" <dkr[ at ]REMOVETHISmvps.org> 30.01.2006 05:03:58
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

"stalley1995" <stalley1995[ at ]yahoo.com> wrote in message
news:1138571762.591959.143990[ at ]g14g2000cwa.googlegroups.com...
[Quoted Text]
> 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-
>


Re: Macro for adding a sequential number to a template
"Graham Mayor" <gmayor[ at ]mvps.org> 30.01.2006 08:04:25
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

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

stalley1995 wrote:
[Quoted Text]
> 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-


Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 30.01.2006 17:01:32
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-

Re: Macro for adding a sequential number to a template
"Jay Freedman" <jay.freedman[ at ]verizon.net> 30.01.2006 18:41:54
stalley1995 wrote:
[Quoted Text]
> 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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


Re: Macro for adding a sequential number to a template
"Doug Robbins - Word MVP" <dkr[ at ]REMOVETHISmvps.org> 30.01.2006 18:46:08
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

"stalley1995" <stalley1995[ at ]yahoo.com> wrote in message
news:1138638297.014899.191110[ at ]g14g2000cwa.googlegroups.com...
[Quoted Text]
> 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-
>


Re: Macro for adding a sequential number to a template
"stalley1995" <stalley1995[ at ]yahoo.com> 31.01.2006 17:14:06
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-

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net