@Poremsky.com

Tips & Tricks for Windows, Office, and Whatever

  • Home
  • Windows
  • Office
  • BlackBerry
  • Web Design
  • WordPress
  • Internet Explorer

Create sequential numbers or random character keywords

Published on October 15, 2013 Last reviewed on January 23, 2021

I receive many requests for help in adding numbers and codes to Outlook subject lines. Some users want to sequential numbers, such as you'd use with invoices, others want to use random numbers or random characters for tracking purposes.

These code samples are basic VB and will work in many situations, in any Office application, or in VB (if anyone still uses VB outside of Office applications).

My examples display the number or random characters in a message box, but you can use it anywhere you'd use a string or variable.

Sequential numbering

With sequential numbering you have two options: save the number in the registry or save the number in a text file. Saving the number in a text file allows you to use it on multiple machines, or shared with other users. Storing it in the registry means you don't have a text file laying around.

This code sample adds the value to the registry, at HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Outlook\Invoices (You can change the sAppName, sSection, and sKey names in the code, if desired.)

Sequential numbers are stored in the registry

 Sub AddInvoiceNumber()

 Dim sAppName As String
 Dim sSection As String
 Dim sKey As String
 Dim lRegValue As Long
 Dim lFormValue As Long
 Dim iDefault As Integer
 sAppName = "Outlook"
 sSection = "Invoices"
 sKey = "Current Invoice Number"

 ' The default starting number.
     iDefault = 101 ' adjust as needed

 ' Get stored registry value, if any.
     lRegValue = GetSetting(sAppName, sSection, sKey, iDefault)

 ' If the result is 0, set to default value.
     If lRegValue = 0 Then lRegValue = iDefault

 ' Increment and update invoice number.
     SaveSetting sAppName, sSection, sKey, lRegValue + 1

' do whatever with the random number
 MsgBox  CStr(lRegValue)

 End Sub

Random numbering

This example generates a random number between 1 and 10,000. Change the High and Low numbers to restrict the random number to a specific number range.

 Sub UseRandomNumber()

   intHigh = 10000
   intLow = 1

 Randomize
   intNumber = Int((intHigh - intLow + 1) * Rnd + intLow)

' do whatever with the random number
 MsgBox intNumber 
 End Sub

Random alphanumeric characters

Generate random charactersThis code sample generates a 10 character alphanumeric code.

To create longer (or shorter) codes, change the value in GetRandom(10).

This function generates codes using upper and lowercase letters and numbers. Non-alphanumeric characters are replaced with random uppercase letters or numbers. (See the ASCII table for the character codes.)

Remove the two If iRand > lines to include those characters.


Sub UseRandomCharacters()
' change 10 to another number for longer key
' do whatever with the random number
 MsgBox GetRandom(10)
 End Sub

 Function GetRandom(Count)
 Randomize
 
 For i = 1 To Count
    iRand = Int((122 - 48 + 1) * Rnd + 48)
   'remove non-alphanumeric characters, replace with uppercase or numbers
     If iRand > 90 And iRand < 97 Then iRand = Int((90 - 65 + 1) * Rnd + 65)
     If iRand > 57 And iRand < 65 Then iRand = Int((57 - 48 + 1) * Rnd + 48)
    GetRandom = GetRandom &amp; Chr(iRand)
 Next
 End Function

More Information

Create a serial number (VBOffice.net)
Macro to Increment Invoice Number to New Form Document (MSKB) This macro works in all versions of Office. It's the basis for the AddInvoiceNumber code sample above.

Share this:

  • Facebook
  • LinkedIn
  • Twitter
  • Print
Subscribe
Notify of
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Martin
May 31, 2015 3:01 pm
#150

Hi, very nice! Can you help me to add the sequential Number to all new incoming Messages into the subject?

0
Reply
Diane Poremsky(@dporemsky)
Author
Reply to  Martin
July 2, 2015 10:14 pm
#152

You need to use an ItemAdd or Run a Script macro.
See http://www.slipstick.com/outlook/email/add-a-file-number-or-keyword-to-the-subject-line-of-messages/#seq for a run a script macro.

0
Reply
Marcus
April 14, 2014 9:58 pm
#104

Hi,

Great article, could you please post a tutorial on how to implement sequential numbering with a TXT file rather than the registry?

Thanks a lot.

0
Reply
Diane Poremsky(@dporemsky)
Author
Reply to  Marcus
April 16, 2014 7:58 pm
#105

Update: my original answer was dumb. 🙂
An example using a text file for the numbers is here:
http://www.vboffice.net/sample.html?lang=en&mnu=2&smp=15&cmd=showitem

See http://www.poremsky.com/office/add-seqential-numbers-word-document/ - this example creates a word document and saves it using the number as the file name.

0
Reply

Recent Posts

  • Change Password or Pin Complexity after removing from Intune
  • Server drafts location is not valid error message
  • What is the Brown File Box Icon on Files in Windows 11?
  • Remove Office Licenses from Windows
  • Add the Recent Items Folder to Windows 10 Quick Access

Recent Comments

  • FUAD on Remove Office Licenses from Windows
  • Brian DuBridge on Add the Recent Items Folder to Windows 10 Quick Access
  • Renee Moodie on Remove Office Licenses from Windows
  • John P on Remove Office Licenses from Windows
  • Puja on Remove Office Licenses from Windows
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

© 2023 · @Poremsky.com

wpDiscuz
Go to mobile version
You are going to send email to

Move Comment