Auto sequential number to a document - For Invoice

I

Irshad Alam

This is in reference to the earlier posted Question and Answer related to
same topic, based on that I tried to do and failed.
I visited the below site, copied the codes, Open a new document create a
form type document and inserted Bookmark and a Macro named "AutoNew" and
pasted the code. Save the document as a Template.
http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm

When I open the template file, first time it works fine, It puts the first
number as 001 and save the file as Path001.
But when I open the document second time onwards, it produces the same
number (001) instead of 002 onwards.

I dont know where is the mistake I am doing. The code I have pasted is as
follows :

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, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub


I am using this on Windows XP operating system and MS office 2000.

Please advise me.

Regards.

Irshad Alam
Abu Dhabi -UAE
 
K

Kathy

Hi

I pasted the code into Word 2000 on Windows 2000, added Dim Order As Variant
under Sub AutoNew() and it works fine (it starts as a string and becomes an
integer).

To make it work as intended, you have to save it as a template (*.dot), then
create a NEW document from it each time (it uses AutoNew) by going File,
New, and double-clicking the template you saved. Then it will create and
save path001.doc in My Documents. The you would finish this document and
re-save it. To make path002.doc, you must do File, New again (or run the
macro manually).

Does the user have write access to C:\ (to edit settings.txt)? Most don't-
I'd try a different path. Why don't you open the template and step through
the code with F8. Then you can see where it goes wrong. If you stop
pressing F8 AFTER it has written the new value for Order (when THIS line is
highlighted: ActiveDocument.Bookmarks("Order").Range.InsertBefore
Format(Order, "00#") ), you can see whether it has updated settings.txt or
not (open it in notepad).

HTH

Kathy
 
I

Irshad Alam

First of all thank you very much for your tips. I tried you said method and
it worked.

Additional Question related to it, I want to bring auto increment number at
the place the cursor is. Not only at the bookmark place and all documents,
whether its template or normal. While typing I may need this increment number
and I want to place the value exactly at the place of my curso.

What changes I should do on the code to do this.

Please advise.
 
D

Doug Robbins

Use Selection.InsertBefore instead of
ActiveDocument.Bookmarks("bookmark").Range.InsertBefore

(Don't have access to the internet while writing this so can't check on
exactly what the original code is.)

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

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