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

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.Insert Before 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.Insert Before 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

ActiveDocument.Bookmarks("Order").Range.InsertBefo re
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.InsertBefo re _
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.

On Fri, 5 Oct 2007 16:46:02 -0700, snulton
wrote:

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.

"Jay Freedman" wrote:

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

On Fri, 5 Oct 2007 16:08:01 -0700, snulton
wrote:

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?

"Jay Freedman" wrote:

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

See
http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm.

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