Solved

Add Random Number to Word

Posted on 2014-09-25
9
2,582 Views
Last Modified: 2014-10-02
Hello,

I want to add a random number, preferably six digits, to a Word document that I print out for customers.  Sequential would be fine, but the point is to add this number and have it be different every time I print.  That way, each customer sheet has a unique number and I will print out a corresponding ticket to give to the customer for pick up purposes.  This will avoid any issues with customers picking up their equipment and finding out later that it was not them. :(

How would I do this?  I found out about Macros, but I have never used it before.

Thank you in advance.
0
Comment
Question by:Scott Thompson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:Montoya
ID: 40344186
do you want the random number in the filename, or do you want it somewhere INSIDE of Word??
0
 
LVL 8

Author Comment

by:Scott Thompson
ID: 40344205
I want it displayed on the sheet.  I'll upload a picture of the sheet and mark where I want it.

Random Number?
0
 
LVL 19

Expert Comment

by:Montoya
ID: 40344252
one more question. Do you truly want a random number? A random number can repeat. Don't you want to avoid duplicates?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 333 total points
ID: 40344254
Generating a random six digit number in VBA is relatively easy in VBA.
 MyNumber = Int(Rnd * 1000000)

Open in new window

The difficult bit would be to ensure that the number has not been used before. This means that you must have a record of what numbers have already been used. The best place for that would be a database. Do you have Access?
0
 
LVL 8

Author Comment

by:Scott Thompson
ID: 40344263
I do not have Access.  I didn't post it before, sorry about that.  I have Word 2010.

How do I insert this code into Word?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 333 total points
ID: 40344840
You should put the code in the ThisDocument module of your Template. Here is the code in a procedure.
Sub Document_New()
    Dim lRand As Long
    Randomize 'see the randomisation process with the current time
    lRand = Int(Rnd * 1000000)
    ActiveDocument.Bookmarks("bmkNumber").Range.Text = Format(lRand, "000000") 'format to keep any leading zeros
End Sub

Open in new window

You will need a bookmark named 'bmkNumber' at the place where the number is to appear. The code runs when you create a new document (File/New).

The code does not attempt to weed out duplicates. For that you will need to keep a list of previously used numbers somewhere. It could be in the template as document variables or as text another Word document, but if there are many numbers, performance wouldn't be very good. If you have Excel, you can use that as a poor man's database.

Your document looks like a paper form. If it is to be completed in Word, you might want to create an electronic version by inserting content controls at the places where data is to be entered. Data from these controls could be entered in the Excel sheet so the the document number could be associated with other details such as the customer name
0
 
LVL 8

Author Comment

by:Scott Thompson
ID: 40345000
Thank you for the code.  I will give that a try.  But you did say that this code would be for when you create a new document.  I want this code to run when I open this document, so everytime I print I have a different number.  Do you know if that is possible?
0
 
LVL 33

Assisted Solution

by:Rob Henson
Rob Henson earned 167 total points
ID: 40346293
This sounds like a case for a mail merge style document.

The data records would be in a table of some form, Access would be ideal as you can then set a Primary Key to each record, Excel would be good as you could generate a new number for each entry with a formula, another Word document with a table would be OK but more manual effort needed for the Unique number element.

When using Mail Merge, you would populate the Data Table with the required details of the new document and then run a merge routine in the template document and select only the new entry to print.

I realise this is probably a lot of work to implement into an existing process/document but could be worth it in the end.

Thanks
Rob H
0
 
LVL 8

Author Closing Comment

by:Scott Thompson
ID: 40358609
Thank you everybody for your suggestions.  I have other projects I'm working on, so I'm closing this for now. I'll give those a try when I have time.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question