How do I set up automatically suceeding numbers in Word?

G

Guest

I have a template that has a space for a number. I want each successive
document opened using that template to have a new number that is the next
number in sucession, i.e. - after opening 20071001, the next time that I
access that template should open 2001002.
 
G

Guest

I have set up the Macro as on the page however when I attempt to open the
template Microsoft Basic pops up with the messege "The macros in this project
are disabled. Please refer to the online help or documentation of the host
application to determine how to enable macros." then I got a popup trying to
debug the first of the last two commands.

Also is the value "00#" a locked in numeric value or will it work with eight
digits?
 
J

Jay Freedman

Did you set up the macro in a template stored in your Templates
folder? Word really doesn't like macros in documents, or in templates
that aren't in the Templates folder, because that's a favorite way to
spread macro viruses.

Assuming it is in a template, go to Tools > Macro > Security and set
the security level to Medium. On the second tab of the dialog, check
the box for "Trust all installed templates and add-ins". That should
make sure the macros are enabled.

The number-format pattern will work with eight digits; just change it
to "0000000#".
 
G

Guest

It was'nt in my Templates folder because I was testing it out before I put it
on my Server which is where I store all my templates related to my business.
I tried to preset the start number to the next one that we need
(i.e."2007100#") but it did not open a successive number it opened a document
with a number 2007100120071001. The same thing happened when I had only the
three digit number 001 turned into 001001 and it was that way on each one I
opened.
 
J

Jay Freedman

I think you're misunderstanding the "00#" expression. That is just
supposed to represent the form of the number -- how many digits to
display -- and not the number itself. You don't change that expression
to the start number.

From the example you gave, I suspect what you're trying to do is
create a number where the first four digits are the year, the next two
are the month, and the last two are a sequence number that starts over
at 1 for each month. If that's true, you're going about it wrong -- if
you put the fixed string "200710" at the beginning, that's fine for
October 2007 but it will be wrong for November, and even more wrong
next year. The macro can take care of the year and month for you with
just a little extra programming. Replace the lines

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

with these lines (to avoid mistakes, use copy/paste instead of trying
to retype):

ActiveDocument.Bookmarks("Order").Range.InsertBefore _
Format(Now, "yyyyMM") & Format(Order, "0#")
ActiveDocument.SaveAs FileName:="path" & _
Format(Now, "yyyyMM") & Format(Order, "0#")

Note that you have to replace "path" in the third line with the actual
path to the folder where you want the document to be saved.

The other thing is that, on the first day of each month, you have to
delete the Settings.txt file and let the macro create a new one that
contains the number 1, so the number in the first document will be
like 20071101.
 
G

Guest

I understood that, but what was happening when the macro was set to 00#, the
first number came out 001. The second time I opened the template the number
read 001001. As far as the date was concerned, I was only trying to establish
a starting point like not starting a checking account with the first check
number 001.

Thanks for your helping a "dumb" contractor. : - }
 
G

Guest

Sorry that it has taken so long to get back to you with the on going
challenge, I was out of town last week and finally got caught up.

After setting up the Macro, upon opening a document I get a variety of error
messeges one of which says "Compile error: Expected End Sub"

This is what I have set up:


Sub MAIN()
'
' AutoExec.MAIN Macro
' Macro created 10/5/2007 by Scott Nulton
'
Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", "MacroSettings",
"Order")

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

System.PrivateProfileString("C:\.txt", "MacroSettings", "Order") = Order

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



End Sub
 
J

Jay Freedman

Hi Scott,

In VBA, every "Sub" statement must have a matching "End Sub" statement, with
all the commands of the macro in between. You have a "Sub MAIN()" statement
without any matching "End Sub". Delete the "Sub Main()" line (and optionally
also delete the four lines of comments that follow it). Then the "Sub
AutoNew()" will match the "End Sub" at the bottom of the code, and that
should stop the error message.
 
G

Guest

I have almost completely started over due to receiving a Microsoft Visual Basic
Run-time error `5941'
The requested memer of the collection does not exist.

When I click on the Debug button the line reading
ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
is hi-lited.
 
J

Jay Freedman

That error is telling you that VBA couldn't find any bookmark named "Order"
in your document. Look again at the article -- the second paragraph begins
"In the template from which you create the document, insert a bookmark named
Order in the location where you want the sequential number to appear."
 
G

Guest

Yes! I forgot the bookmark. Will the sequencial numbering work if I lock the
form when savesd as template?
 
G

Guest

I now recieve a Run-time error `4605`
This method or property is not available because the object refers to a
protected area of the document.
 
J

Jay Freedman

You can do either of two things:

- In the template, insert continuous-type section breaks before and after
the paragraph containing the bookmark. When you protect the document (using
the Tools > Protect Document menu item, not the lock icon on the Forms
toolbar), click the "Select sections" link and uncheck the section that
contains the bookmark.

- Or, in the macro, insert new code before and after the InsertBefore line
like this:

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order,
"0000#")

ActiveDocument.Protect

ActiveDocument.SaveAs FileName:="path" & Format(Order, "0000#")


That will turn off the protection just long enough to insert the sequence
number, and then turn the protection on again.

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

Guest

Thank you! One more thing, how do I have different documents have their own
sets of sequencial numbers? Right now the sequence is working but I am using
the same "Order" in four different documents, two different contracts, one
bid proposal and one invoice.
 
J

Jay Freedman

First, you have to use a different template for each sequence. Since
you're doing forms of different kinds, I assume that's already true.

Then the other part is to visit the macro code in each template and
put in a different name for the file that stores the current number.
Now they all say "C:\Settings.Txt" in two place (at least I hope they
do -- the code you posted before has "C:\.txt" in the second place,
which is wrong).
 
G

Guest

Yes, each is a different template for a different kind of Word document.
Sorry to be so dense, but could you give me an example of the different
names? I am not sure that I understand. Is there something that I could
download that explains it so I don't bother you further?
 
J

Jay Freedman

No download, you're off in an area that's sort of "you just need to
understand what you're doing". :)

Exactly what you use for the names isn't terribly important as long as (a)
they're valid file names, (b) there's a different one for each template, and
(c) you can recognize them when you see them.

So I'll suggest that for each one you take the name of the template
(assuming that's something you recognize as being associated with that type
of form), add the word "sequence" and the extension ".txt". For example, if
you have a template named "Contract A.dot" and one named "Contract B.dot",
then the corresponding names inside the macro could be "Contract A
sequence.txt" and "Contract B sequence.txt". Put a valid path in front of
each name, for example "C:\Contract A sequence.txt", and put that into the
macro in the template for Contract A. Put "C:\Contract B sequence.txt" into
the macro in the template for Contract B. Do similarly for the other
templates -- each one gets a filename in its macro that corresponds to that
template only.

Am I making sense yet?
 
G

Guest

If I have it correctly, then:

If the original Macro that you showed me:


Sub AutoNew()

Order = System.PrivateProfileString(“C:\Settings.Txtâ€,â€MacroSettingsâ€,â€Order)

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

System.PrivateProfileString(“C:\Settings.txtâ€, “MacroSettings,â€Orderâ€) = Order

ActiveDocument.Bookmarks(“Orderâ€).Range.InsertBefore Format(Order. “0000#â€)
ActiveDocument.SaveAs FileName:=â€path†& Format(Order, “0000#â€)

End Sub

Then the modification would be:

Sub AutoNew()

Order = System.PrivateProfileString(“C:\RemContract
Sequence.Txtâ€,â€MacroSettingsâ€,â€Order)

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

System.PrivateProfileString(“C:\RemContract
Sequence.Txtâ€,â€MacroSettingsâ€,â€Order) = Order

ActiveDocument.Bookmarks(“Orderâ€).Range.InsertBefore Format(Order. “0000#â€)
ActiveDocument.SaveAs FileName:=â€path†& Format(Order, “0000#â€)

End Sub


Is this correct?
 
J

Jay Freedman

Yes, that's correct. :)
If I have it correctly, then:

If the original Macro that you showed me:


Sub AutoNew()

Order =
System.PrivateProfileString("C:\Settings.Txt","MacroSettings","Order)

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

System.PrivateProfileString("C:\Settings.txt",
"MacroSettings,"Order") = Order

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

End Sub

Then the modification would be:

Sub AutoNew()

Order = System.PrivateProfileString("C:\RemContract
Sequence.Txt","MacroSettings","Order)

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

System.PrivateProfileString("C:\RemContract
Sequence.Txt","MacroSettings","Order) = Order

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

End Sub


Is this correct?
 
G

Guest

I get an error messege that I will be able to retrieve on monday to send to
you. Have a great weekend. :-}
 

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