Share via

VBA code correction for AutoIncrement number

Anonymous
2012-07-17T08:32:50+00:00

I am trying to write a code to get a auto increment number for my document at the curson position :

The below code gets the correct result but at the last of the document. My requirement is that where ever I may be in the document , It shoud brng the result there, I mean at the cursor position :

Sub AutoNew()

'

' AutoNew Macro

' Macro created 17-07-2012 by mia

'

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.Range.InsertAfter Format(Order, "00#")

End Sub

Please advice the correct code as according to my need.

Regards

Irshad

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-07-17T09:15:27+00:00

A macro named AutoNew is intended to run automatically whenever you create a new document based on the document or template that contains the code.

If you want to insert the order number in the current location, it should be an ordinary macro, preferably not named AutoNew.

For example:

Sub InsertNumber()

    '

    ' AutoNew Macro

    ' Macro created 17-07-2012 by mia

    '

    Dim Order

    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

    Selection.InsertAfter Format(Order, "000")

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2012-07-17T09:23:04+00:00

What you should have is

Sub AutoNew()

Dim order As Long

order = 0

If Dir("C:\temp\Settings.Txt") <> "" Then

    order = System.PrivateProfileString("C:\temp\Settings.Txt", _

                                        "MacroSettings", "Order")

End If

If order = 0 Then

    order = 1

Else

    order = order + 1

End If

System.PrivateProfileString("C:\temp\Settings.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.htmuseful.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2012-07-17T09:06:47+00:00

    Selection.Format(Order, "00#")

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-07-17T08:59:43+00:00

    Sir,

    Thanks for your reply. I tried the below line and getting error

    Selection Format(Order, "00#")

    May be I am writing the code wrong way, I am not good in VBA, could you please complete the line of selection as per my requirement.

    Regards

    Irshad

    Was this answer helpful?

    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2012-07-17T08:39:07+00:00

    Change:

    ActiveDocument.Range

    to:

    Selection

    Was this answer helpful?

    0 comments No comments