Vba Auto Increment File Name

renewima
2 min readNov 6, 2021

Download here

  1. Excel Vba Increment Integer
  2. Excel Vba Auto Increment Id Number

Change the path to the text file you created. Close the VBE and save the document AS A TEMPLATE (‘Your File Name.dot). Close the file. Create a blank Word file and save it as a text (*.txt) file in the location designated in the code; close it. Test the code: From Word’s File menu select File > New and select your template.

Active2 years, 3 months ago

We’re developing an invoice template for Word, which contains an invoice number in a field at the top of the page.

We would like the invoice number to auto-increment (from the last invoice number), and we would like the filename to be based on the invoice number.

They’ll all be saved in the same directory.

SteveSteve

1,2581111 gold badges4848 silver badges9494 bronze badges

2 Answers

Donald W GartonDonald W Garton

I found this which I think will solve the problem:

Use an Autonew macro to add a sequential number to a document and save it with that number.

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 and create an AutoNew macro in the template, as follows:

If you don’t need to display the number in the document, but just want to save it with a sequential number, there is no need to create the bookmark in the template and you should then delete the second last line of the code.

DavidPostill♦

114k2727 gold badges254254 silver badges284284 bronze badges

SteveSteve

Excel Vba Increment Integer

1,2581111 gold badges4848 silver badges9494 bronze badges

Not the answer you’re looking for? Browse other questions tagged microsoft-wordmicrosoft-word-2007macros or ask your own question.

Excel Vba Auto Increment Id Number

What you should have is
Sub AutoNew()
Dim order As Long
order = 0
If Dir(‘C:tempSettings.Txt’) <> ‘ Then
order = System.PrivateProfileString(‘C:tempSettings.Txt’, _
‘MacroSettings’, ‘Order’)
End If
If order = 0 Then
order = 1
Else
order = order + 1
End If
System.PrivateProfileString(‘C:tempSettings.txt’, ‘MacroSettings’, _
‘Order’) = order
Selection.Range.InsertAfter Format(order, ‘00#’)
End Sub
Note that in some operating systems e.g. Windows 7 you may not be able to write to the root of the C drive so an alternative pre-existing folder would be advisable as above.
The macro now also checks for the presence of the settings ini file.
The changes are highlighted in bold
HOWEVER! This macro runs when you create a new document from the template containing it, so the Selection Range is always going to be at the end of the document. If you want the number to go elsewhere you need either to write the value to a bookmarked location pre-inserted in the template or to a document variable and display the variable with a docvariable field.
If you want to be able to manually insert the next number at the cursor then change the macro name from autonew to (e.g.) InsertMyNumber
You may find http://www.gmayor.com/automatic_numbering_documents.htm useful.

Download here

--

--