View Single Post
  #25   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default How do I set up automatically suceeding numbers in Word?

when I attempt to set the AutoNew for a specific
template and I edit what is opened with AutoNew to reflect that
template it changes all the other templates to that Macro.


That _should_ be impossible, if you're working correctly in the VBA editor.

In the VBA editor, there should be a panel on the left side, titled Project.
(If you don't see it, click View Project Explorer or press the shortcut
Ctrl+R). It's a tree diagram that shows open templates and documents as
folders, labeled as "Project" or "TemplateProject" plus the name of the file
it represents. Expand the icon (by clicking the plus sign) for the
particular template you're working on, and you should see a "Microsoft Word
Objects" folder and a "Modules" folder. Expand the "Modules" folder, and
under that you should see at least one item, possibly named "Module1" or
"NewMacros" or something else. When you double-click this module, you should
see the AutoNew code in the big window on the right.

If you don't see what I just described, then the AutoNew macro is probably
in a module under the Modules folder in the Normal project, which represents
the Normal.dot template. THAT'S THE WRONG PLACE. A module in the Normal.dot
template is NOT in the other templates. The Normal.dot template loads when
Word starts, and its macros are available in every document regardless of
what template the document is based on, so an AutoNew in Normal "appears" to
be present for every template. But that's not what you want -- you want a
separate AutoNew in each of your templates and NONE in Normal.dot.

If that's where it is, follow these steps:

- Click (to select) the "Modules" folder under the form template you're
working on (for example, "NRS RemContract.dot").
- On the VBA Editor menu, click Insert Module. Notice the new module icon
in the tree diagram.
- Double-click the module under the Normal project that contains the AutoNew
macro code. Select that code and cut it to the clipboard. You don't want to
leave any AutoNew code in the Normal template.
- Double-click the module icon you created in the tree under your template's
icon. Paste the code from the clipboard into the big empty window on the
right.
- Make sure the file names we've been discussing match the name of the
template you're working with.
- Click the Save toolbar button, which saves this template.
- Close your template and try using File New. Remember, File New is
necessary to run the AutoNew macro.

Now you should be able to open another of your templates, create a module
under its project icon, paste in a copy of the code, change the file names,
and save. That should operate independently of the first template.

snulton wrote:
I followed the items you laid out and when I open the next template
to modify it the AutoNew is already set. When I make the
modifications to that Macro for the open document, it resets all the
other templates to that sequential numbering.

As per Item 3: I hadn't been doing that but even when I do I get the
sequence of numbers that is continuos no matter which template I open
If I open one template that has a number of 01005, then open a
different template that is suposed to have a number starting 03*** it
will open with 01006.

Although all these are separate templates, the AutoNew seems to treat
them the same. when I attempt to set the AutoNew for a specific
template and I edit what is opened with AutoNew to reflect that
template it changes all the other templates to that Macro.

"Jay Freedman" wrote:

Item 1: DO NOT change the name of the macro. It has to be named
AutoNew() in order to work.

Item 2: In the template named "NRS RemContract.dot", there should be
an AutoNew macro that contains the lines (each of these should be one
line, but the posting program breaks them):

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

and

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


In the template named "NRS Service & Repair.dot", there should also
be a macro named AutoNew, and it should contain the lines

Order = System.PrivateProfileString("C:\NRS Service & Repair
Sequence.Txt","MacroSettings","Order)

and

System.PrivateProfileString("C:\NRS Service & Repair
Sequence.Txt","MacroSettings","Order) = Order


In the template named "NRS Bid-proposal.dot", there should also be a
macro named AutoNew, and it should contain the lines

Order = System.PrivateProfileString("C:\NRS Bid-proposal
Sequence.Txt","MacroSettings","Order)

and

System.PrivateProfileString("C:\NRS Bid-proposal
Sequence.Txt","MacroSettings","Order) = Order

Item 3: (Just in case you aren't already doing this...) You have to
use the File New command (and if you get a task pane, click the
link for "On my computer") and select the proper template as the
base for a new document. The File New command is what causes the
macro named AutoNew to run, and that does all the sequence number
stuff. If you don't do File New, the macro doesn't run and you
don't get a sequence number.

Are we there yet? ;-)

On Mon, 22 Oct 2007 16:11:01 -0700, snulton
wrote:

I have tried to set up each of three different contracts that I
have set as templates (.dot) as individually numbered documents
using the names for "NRS RemContract.dot" with the Order Macro set
as below, with a new Macro named as RemContractOrder (replacing
"Order" with "RemContractOrder") and nothing seems to work to give
me a different numbering system for my three templates. The
templates are named; NRS RemContract.dot, NRS Service & Repair.dot,
and NRS Bid-proposal.dot. Please help a dumb Remodeling Contractor
get this right.

"Jay Freedman" wrote:

Yes, that's correct. :-)

snulton wrote:
If I have it correctly, then:

If the original Macro that you showed me:


Sub AutoNew()

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

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

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

ActiveDocument.Bookmarks("Order").Range.InsertBefo re 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.InsertBefo re Format(Order.
"0000#") ActiveDocument.SaveAs FileName:="path" & Format(Order,
"0000#")

End Sub


Is this correct?

"Jay Freedman" wrote:

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?

snulton wrote:
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?

"Jay Freedman" wrote:

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).

On Thu, 18 Oct 2007 14:34:00 -0700, snulton
wrote:

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.

"Jay Freedman" wrote:

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.InsertBefo re
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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to
the newsgroup so all may benefit.

snulton wrote:
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.

"Jay Freedman" wrote:

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."

snulton wrote:
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.InsertBefo re
Format(Order, "00#")
is hi-lited.

"Jay Freedman" wrote:

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.

snulton wrote:
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.InsertBefo re
Format(Order, "0000#")
ActiveDocument.SaveAs FileName:="path" & Format(Order,
"0000#")



End Sub



"Jay Freedman" wrote:

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