Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate Random Number in Word

Status
Not open for further replies.

mhypolite

Vendor
Feb 5, 2005
59
0
0
KY
Hi guys, I have a customer that wants a unique random number generated in the header of a document for tracking purposes, is there any way to use VBA code to do something like that? Was thinking some way to use the date and time down to seconds to create the number so it will end up something like this MMDDYYTIME e.g (040707063012)

Or any other Idea or method I can use to get the same goals


Thanks for the help guys
 
Which do you want? A random number - in which case you can use the Rnd function; or a calculated number, like your date/time idea?

Gerry
 
unique random number

The concept is oxymoronic. If it is truely random then you cannot ensure that it will be unique.

If you test for uniqueness then it is no longer random.

Your time idea does not guarantee a unique number, because you cannot be sure that it is impossible to create more than one document in the same second.

 
Well put.

Please note that, as mintjulep hints at, the Rnd function does produce a random number, but it CAN, during the course of using it, produce a number it has produced previously. Therefore, each number returned by the function is not strictly speaking unique...but it IS random.

The date/time scenario of course involves some sort of calculation. Therefore it too can (depending on what you do as calculation) produce non-unique numbers.

Perhaps if you tell us more of what you want to happen?

Gerry
 
The simplest way to get unique numbers is to just count incrementally.

Store the last number used in a .ini file, or as a doc variable.
 
Microsoft already stick a unique ID number into each Word document, and it is a shame we don't have access to it. However, we can use the same technique as they do to generate it, which is to generate a UUID:
Code:
[blue]Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGUID As GUID) As Long
Private Declare Function StringFromGUID2 Lib "OLE32.DLL" (pGUID As GUID, ByVal PointerToString As Long, ByVal MaxLength As Long) As Long

Private Type GUID
    Guid1 As Long
    Guid2 As Integer
    Guid3 As Integer
    Guid4(0 To 7) As Byte
End Type

Public Function GetUUID() As String

    Dim udtGUID As GUID
    Dim sGUID As String
    Dim lResult As Long

    lResult = CoCreateGuid(udtGUID)

    If lResult Then
        sGUID = ""
    Else
        sGUID = String$(38, 0)
        StringFromGUID2 udtGUID, StrPtr(sGUID), 39
    End If

    GetUUID = sGUID

End Function[/blue]

 
Mike,

Certainly a GUID can be used as a unique identifier but what evidence do you have that says Word sticks one in every document? It may add one to every Project but I don't believe it adds one to documents without code.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tools/Options/Security/Store Random Mumbers to Improve Merge Accuracy
 
(Just as a point of interest it was the unannounced storage of a GUID in every Word document and the fact that the GUID used to contain the MAC address of the PC the document was created on that got a lot of people upset at Microsoft back in 1999 (and Office 97 didn't have an option to remove it))
 
Thanks For the replies guys, the client really wanted ah way of tracking documents via a Number in the header, these documents will be stored in sharepoint, that indexes the body and header, it is a law firm, so if they email you a document, the customer can references the document via that number in further conversations, and the lawyers can quickly search sharepoint for that number and pull the document
 
Thanks, Mike,

I don't know what that option does. I have searched and can find nothing beyond the fairly bland statement in Word help - other people quote or paraphrase it but that's as far as it goes. Nowhere can I find any explanation of what the number may be, or how it may be generated or used. If it were truly random it could not, by definition, be used as claimed.

I know there was a fuss about Word 97 storing GUIDs which allowed identification of the machine - but don't know a great deal of detail. I believe GUIDs are now created using a different algorithm but whether or not it uses the MAC address I don't know; to have a reasonable chance of being globally unique, of course, some machine-dependent seed is presumably still used.

I created a Word document and saved it. I took a copy of it and saved it (from Word) with the same name in a different location. Comparing the two documents shows the only difference to be a (Windows) timestamp buried in there. I switched off the 'random number' option and repeated the test and got exactly the same result. Checking on different machines (with a combination of Word (not 97) and Windows versions produces the same result. So where is this unique number (Windows timestamps in my tests are, of course, unique) when the option is switched on - and where isn't it when it is switched off?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
>I believe GUIDs are now created using a different algorithm

Correct. GUIDs can still be created the old[sup]1[/sup] way, with the MAC address, but the main API calls (UUIDCreate and CoCreateGUID) no longer include it


1 Old in the sense that it is no longer the primary way Windows creates UUIDs, but not obsolete as it remains part of the UUID specification (RFC4122)
 
Ok, so you need a unique number, not a random one.

I don't know what sharepoint is. Is there a way that it can assign the numbers? As I wrote earlier, the simplest way to get a unique number is just to count. So each time a new Word document is created, ask sharepoint what the last number used was, add 1 and away you go.
 
Sharepoint is Microsoft versions of a document management system, and yes it can, but that’s another problem, I wrote an event sync to catch anytime a new document is saved to sharepoint and it creates the unique number for me number for me, but sometimes, Microsoft word overwrites this number and leaves it blank if it did not detect the field already had data in it.
 
What a riot! That is Word for you.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top