#1
|
|||
|
|||
Random number difficulties
Hello,
This is a rather lengthy problem, so please bear with me. I am working on creating a document with two random number fields. I have used excel's "RAND" command to create the random number itself ( 6 digit integers from 000000 to 999999 ) and have inserted the cell as an object to my word document. I have two fields needing this number ( one on each page ). My problem is that I need the number to be the same in each field. If I insert the object onto both pages, I get two separate numbers. I tried copying the cell and pasting it to my document as a link to the original excel workbook. This helps to a point, so long as I have both files open at the same time. Then I can recalculate in Excel, print in word, and repeat the process several times. If I close either document, though, I fail to get a new number, or the numbers don't match in the two fields. Ideally, I'd like to end up with a two page document that I can set to print a couple hundred copies with a different number on each copy, but the same number on each two page set. For example page one has "123456" and page two has "123456," then the next print of page one would have "321465" and page two would have the same "321465". I hope to end up with several unique two page sets, so that upon distribution, I can give page one to my clerk and page two to my client and they each have the same number at the bottom. The next set would have a different number for the new client. I'm not even sure if this is possible in office or how to accomplish it if it is. Any help would be greatly appreciated, and I hope that this made sense. |
#2
|
||||
|
||||
hi ahrinn,
See if the attached does what you want. Basically, the document has a formfield that calls a macro to put a number in it. A cross-reference field then replicates the number at another location. No Excel involvement.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
That looks like it may work, but how would I set the macro up in my document? Also, does it generate a different number each time it's opened? I'd love to be able to print a couple hundred copies with a different number on each copy, but I wasn't sure if that would work. Sorry for all the questions, my office experience and needs aren't usually this in depth. Thanks a lot. |
#4
|
||||
|
||||
hi ahrinn,
Do you want the number to update automatically when you open the document, or only when you decide? Do note that, whichever way you go, there is a small risk that the generated number will be the same as one you've used before. At present it does the latter - simply pressing the tab key updates it. The macro is quite simple, but it needs to be coded differently - and perhaps the document designed differently, according to what you want.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Could we try each way and see which ends up working better? Only if that's not too much trouble. Thanks again for all your help.
Quote:
|
#6
|
||||
|
||||
Hi ahrinn,
See attached. On opening the document, you'll get a prompt to allow macros to run. If you do so, you'll then get asked whether to update the random number. If you answer Yes, it'll be updated. To update the number afterwards, press Alt-F8, then click on 'UpdateRandomNumber', OK. If that's what you want, I'll explain how to implement it.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
That looks just about right. Could it be made to update on opening without asking permission? Otherwise, it looks just about perfect. Thanks again for all your help.
Quote:
|
#8
|
|||
|
|||
I do notice that it seems to pull numbers from a set list. Every time I open it and agree to update the number, I get the same number. If I tell it to update when already open, it seems to come up with the same numbers in the same order. Is there a way to get around this? Out of curiosity, if I told it to print 100 copies of the document, would there be a way to get a different number on each document, or would it print 100 copies with exactly the same number? If I'm asking for the impossible, please let me know. Thanks again for all your help and knowledge.
|
#9
|
||||
|
||||
Hi ahrinn, to create a document with fully-random numbers being generated automatically upon opening:
• create the document; • press Alt-F11 to open the vba environment; • on the left you'll see your document's name. Expand that and you'll see a 'ThisDocument' entry. Double-click on that. • in the 'ThisDocument' module, insert the following code - Code:
Private Sub Document_Open() Call UpdateRandomNumber End Sub Sub UpdateRandomNumber() Randomize Timer With ActiveDocument .CustomDocumentProperties("RandNum").Value = Format(Int(1000000 * Rnd), "000000") .Fields.Update End With End Sub • add a numeric custom document property named 'RandNum' to your document, with a value of 0. To do this, use the Pizza Button, then choose Properties|Advanced|Custom. • wherever you want the random numbers to appear, insert a DOCPPROPERTY field coded as {DOCPPROPERTY RandNum}, where the field braces (ie '{}') are created via Ctrl-F9.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
||||
|
||||
Cross-posted at: http://answers.microsoft.com/en-us/o...3-e33b61363140
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184 It is extremely poor form to post a question relating to a topic you've already been given considerable help for without aknowledging or referencing the help you've already received.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
I apologize if I have offended you. I do appreciate all your help.
Quote:
|
#12
|
||||
|
||||
It's really not an issue of offending someone, but of wasting peoples' time. Those who make a habit of cross-posting without either providing links or acknowledging the fact they've already received help on an issue very soon find no-one willing to help them. I have also known a number of people to be banned from some forums for repeatedly breaching this basic convention.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Okay, document is created and successful. Thanks again for your help and instruction.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
random pop up wont go away | 464646 | OneNote | 0 | 05-01-2012 04:34 AM |
how can i make random variant | neezeen | Word | 0 | 11-22-2011 08:08 AM |
Random number generator for Powerpoint 2011 | kala85 | PowerPoint | 0 | 11-19-2011 06:16 AM |
Visio random crash? | maxoys45 | Visio | 5 | 10-25-2010 09:15 AM |
Random names from a given list | professor snape | Excel | 1 | 06-06-2009 09:39 AM |