Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
Tony
 
Posts: n/a
Default Populating a Label/Text Box with Random Number

I have put a txt box and a label in a word doc. I am trying to populate it
with code that I have written to generate a random number and I am not having
any luck.

Is there a property that I am not aware of for labels and txt boxes in word.

For all I know it could be my Option Explicit code at the top. Here is my
code:
It starts in the Word Project:

Option Explicit

Public Function GenPrimaryValue() As String
Dim strStart As String
Dim strDate As String
Dim strEnd As String
Dim CrntDate As Date
Dim intChar As Integer
Dim upperbound As Long
Dim lowerbound As Long
Dim X As Long
Randomize

CrntDate = Now
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

strStart = Chr(intChar)

strDate = DatePart("YYYY", CrntDate)
strDate = strDate & Format(DatePart("m", CrntDate), "00")
strDate = strDate & Format(DatePart("d", CrntDate), "00")

strEnd = ""

For X = 1 To 5
upperbound = 0
lowerbound = 4
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Select Case intChar
Case 1
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Case Else
upperbound = 51
lowerbound = 48
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Select
strEnd = strEnd & Chr(intChar)
Next X

GenPrimaryValue = strStart & strDate & strEnd
PRONUM = GenPrimaryValue

End Function

Private Sub Document_Open()

Call GenPrimaryValue

End Sub

Can someone someone please tell me what the hang up is?
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Doug Robbins - Word MVP
 
Posts: n/a
Default Populating a Label/Text Box with Random Number

Replace the last line of your function with:

With ActiveDocument
.Variables("ProNum").Value = GenPrimaryValue
.PrintPreview
.ClosePrintPreview
End With

and in the textbox and the labels, insert a { DOCVARIABLE ProNum } field.

When the function is called, the variable ProNum will be set to the
generated number in GenPrimaryValue and the PrintPreview will cause the
fields in the document to be updated so that the generated number is
displayed in the document.

Are you sure that you should not be doing this in a template and using an
autonew macro rather that the Document_Open() event?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Tony" wrote in message
...
I have put a txt box and a label in a word doc. I am trying to populate it
with code that I have written to generate a random number and I am not
having
any luck.

Is there a property that I am not aware of for labels and txt boxes in
word.

For all I know it could be my Option Explicit code at the top. Here is my
code:
It starts in the Word Project:

Option Explicit

Public Function GenPrimaryValue() As String
Dim strStart As String
Dim strDate As String
Dim strEnd As String
Dim CrntDate As Date
Dim intChar As Integer
Dim upperbound As Long
Dim lowerbound As Long
Dim X As Long
Randomize

CrntDate = Now
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

strStart = Chr(intChar)

strDate = DatePart("YYYY", CrntDate)
strDate = strDate & Format(DatePart("m", CrntDate), "00")
strDate = strDate & Format(DatePart("d", CrntDate), "00")

strEnd = ""

For X = 1 To 5
upperbound = 0
lowerbound = 4
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Select Case intChar
Case 1
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Case Else
upperbound = 51
lowerbound = 48
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Select
strEnd = strEnd & Chr(intChar)
Next X

GenPrimaryValue = strStart & strDate & strEnd
PRONUM = GenPrimaryValue

End Function

Private Sub Document_Open()

Call GenPrimaryValue

End Sub

Can someone someone please tell me what the hang up is?



  #3   Report Post  
Posted to microsoft.public.word.docmanagement
Jezebel
 
Posts: n/a
Default Populating a Label/Text Box with Random Number

You code generates random strings happily enough; but it doesn't do anything
with them. If you want these random values to appear in your document, you
need to put them there. Currently they just sit in your VBA code.

Is there any particular reason you want to use labels and textboxes for
this? -- much easier to insert the values into bookmarks or document
properties.



"Tony" wrote in message
...
I have put a txt box and a label in a word doc. I am trying to populate it
with code that I have written to generate a random number and I am not
having
any luck.

Is there a property that I am not aware of for labels and txt boxes in
word.

For all I know it could be my Option Explicit code at the top. Here is my
code:
It starts in the Word Project:

Option Explicit

Public Function GenPrimaryValue() As String
Dim strStart As String
Dim strDate As String
Dim strEnd As String
Dim CrntDate As Date
Dim intChar As Integer
Dim upperbound As Long
Dim lowerbound As Long
Dim X As Long
Randomize

CrntDate = Now
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

strStart = Chr(intChar)

strDate = DatePart("YYYY", CrntDate)
strDate = strDate & Format(DatePart("m", CrntDate), "00")
strDate = strDate & Format(DatePart("d", CrntDate), "00")

strEnd = ""

For X = 1 To 5
upperbound = 0
lowerbound = 4
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Select Case intChar
Case 1
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Case Else
upperbound = 51
lowerbound = 48
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Select
strEnd = strEnd & Chr(intChar)
Next X

GenPrimaryValue = strStart & strDate & strEnd
PRONUM = GenPrimaryValue

End Function

Private Sub Document_Open()

Call GenPrimaryValue

End Sub

Can someone someone please tell me what the hang up is?



  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Tony
 
Posts: n/a
Default Populating a Label/Text Box with Random Number

I don't use Word that much if at all. I am pretty savy in Access and Vb but
not at all in word. I have not used word as a function of my job for
probably over 4 years. So I am not all that familiar with the functions that
are used.

I thought I would be able to create a quick form that generates the AutoNum
I need so that I can send the Doc to an outside person to open fill out the
info then fax to our clients and back to me to update on my side business.

"Jezebel" wrote:

You code generates random strings happily enough; but it doesn't do anything
with them. If you want these random values to appear in your document, you
need to put them there. Currently they just sit in your VBA code.

Is there any particular reason you want to use labels and textboxes for
this? -- much easier to insert the values into bookmarks or document
properties.



"Tony" wrote in message
...
I have put a txt box and a label in a word doc. I am trying to populate it
with code that I have written to generate a random number and I am not
having
any luck.

Is there a property that I am not aware of for labels and txt boxes in
word.

For all I know it could be my Option Explicit code at the top. Here is my
code:
It starts in the Word Project:

Option Explicit

Public Function GenPrimaryValue() As String
Dim strStart As String
Dim strDate As String
Dim strEnd As String
Dim CrntDate As Date
Dim intChar As Integer
Dim upperbound As Long
Dim lowerbound As Long
Dim X As Long
Randomize

CrntDate = Now
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

strStart = Chr(intChar)

strDate = DatePart("YYYY", CrntDate)
strDate = strDate & Format(DatePart("m", CrntDate), "00")
strDate = strDate & Format(DatePart("d", CrntDate), "00")

strEnd = ""

For X = 1 To 5
upperbound = 0
lowerbound = 4
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Select Case intChar
Case 1
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Case Else
upperbound = 51
lowerbound = 48
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Select
strEnd = strEnd & Chr(intChar)
Next X

GenPrimaryValue = strStart & strDate & strEnd
PRONUM = GenPrimaryValue

End Function

Private Sub Document_Open()

Call GenPrimaryValue

End Sub

Can someone someone please tell me what the hang up is?




  #5   Report Post  
Posted to microsoft.public.word.docmanagement
Jezebel
 
Posts: n/a
Default Populating a Label/Text Box with Random Number

In that case, save the random number into a document property, and use a
DocProperty field in the body of the document to display it --

ActiveDocument.CustomDocumentProperties("DocNum") = Format(Now, "yyyymmdd")
& Format(Clng(9999 * Rnd + 1000)




"Tony" wrote in message
...
I don't use Word that much if at all. I am pretty savy in Access and Vb
but
not at all in word. I have not used word as a function of my job for
probably over 4 years. So I am not all that familiar with the functions
that
are used.

I thought I would be able to create a quick form that generates the
AutoNum
I need so that I can send the Doc to an outside person to open fill out
the
info then fax to our clients and back to me to update on my side business.

"Jezebel" wrote:

You code generates random strings happily enough; but it doesn't do
anything
with them. If you want these random values to appear in your document,
you
need to put them there. Currently they just sit in your VBA code.

Is there any particular reason you want to use labels and textboxes for
this? -- much easier to insert the values into bookmarks or document
properties.



"Tony" wrote in message
...
I have put a txt box and a label in a word doc. I am trying to populate
it
with code that I have written to generate a random number and I am not
having
any luck.

Is there a property that I am not aware of for labels and txt boxes in
word.

For all I know it could be my Option Explicit code at the top. Here is
my
code:
It starts in the Word Project:

Option Explicit

Public Function GenPrimaryValue() As String
Dim strStart As String
Dim strDate As String
Dim strEnd As String
Dim CrntDate As Date
Dim intChar As Integer
Dim upperbound As Long
Dim lowerbound As Long
Dim X As Long
Randomize

CrntDate = Now
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

strStart = Chr(intChar)

strDate = DatePart("YYYY", CrntDate)
strDate = strDate & Format(DatePart("m", CrntDate), "00")
strDate = strDate & Format(DatePart("d", CrntDate), "00")

strEnd = ""

For X = 1 To 5
upperbound = 0
lowerbound = 4
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Select Case intChar
Case 1
upperbound = 65
lowerbound = 89
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
Case Else
upperbound = 51
lowerbound = 48
intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Select
strEnd = strEnd & Chr(intChar)
Next X

GenPrimaryValue = strStart & strDate & strEnd
PRONUM = GenPrimaryValue

End Function

Private Sub Document_Open()

Call GenPrimaryValue

End Sub

Can someone someone please tell me what the hang up is?






Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a limit of words you can put in tables? lkwink Tables 4 October 16th 05 03:15 PM
Help - Random Work Order Number Eduardo Microsoft Word Help 0 June 13th 05 09:34 PM
How do I insert chapter number with page number in footer? Joan Microsoft Word Help 4 May 9th 05 04:00 PM
random number creation Toni Microsoft Word Help 1 March 25th 05 08:01 AM
Maximum number of pages in a word document Travis75 Formatting Long Documents 5 January 27th 05 05:03 AM


All times are GMT +1. The time now is 09:04 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"