=rand(x,y) usable in VBA-code ? How ??

H

Hans Troost

Hello all,

For some testing during program development I want to
(programmatically) create a new document and add some or very much
(!!) text to it.

So I tried to use the (undocumented) =rand(x,y) function in my
VBA-program.

What I first did:
Sub Test()
Documents.Add
With ActiveDocument
Selection.TypeText Text:="=rand(3,5)"
End With
End Sub

What happened when running this macro?

1. New document created (fine thus far)
2. With text "=rand(3,5)" in it, in stead of 3 paragraphs with 5 times
"The quick....", so not good. However I can understand that it just
adds the string

3. Thus macro was finished and pressing the <enter>-key once changed
to document to the required result: but I dont want any interactive
interaction!!

So I tried the following:

Sub Test()
Documents.Add
With ActiveDocument
Selection.TypeText Text:="=rand(3,5)"
Selection.TypeParagraph
End With
End Sub


Same result: text =rand(3,5) in the document and same behaviour:
pressing <enter> changes it.

So another try:

Sub Test()
Documents.Add
With ActiveDocument
Selection.TypeText Text:="=rand(3,5)"
Call SendKeys("%{ENTER}")
End With
End Sub

This should do !! But no: now I enter with a document with the
following line:
=rand(3,5)=rand(3,5) (???...)

So my question is: how can I use the =rand(x,y) in a macro?

Regards,

Hans Troost
 
R

Rob Barnard

Hi,

Not sure what you think the rand(x,y) function is supposed
to do but I doubt it's to do with the number of sentences
in a paragraph.

I think the following code will achieve what it appears
you're trying to do:

Sub Test()

Dim sText As String
Dim iRand As Integer

sText = "The quick brown fox... "
iRand = Int(Rnd * 8) + 1

Documents.Add
For iLoop = 1 To iRand
Selection.TypeText Text:=sText
Next

End Sub
 
D

Dave Lett

Hi Rob,

In a blank Word document type:

"=rand(3,5)"

and you'll see what it does. After that, you can change number of paragraphs
and number of sentences in each paragraph by changing the parameters of the
rand function.
 
H

Hans Troost

Hi Larry,
That rnd statement is interesting, producing a random number of repeats
of the text each time. But I don't think that's what Hans was looking
for.
I'm afraid it is not exactly what you think: =rand(x,y) does not
produce something random or a random number of times: it exactly gives
x-paragraphs with y-times "the quick brown....".
And more: thats exactly what I'm interesed in.

Nevertheless I tried your solution: but still got the same behaviour:
the macro still inserts the text "=rand(3,5") and I have to press
<enter> to get the desired result, after the macro finished.

I think - and am afraid - that Martinique is right: it is programmed
complete different than other Word commands, and being "undocumented"
must have a reason...

Hans
 
H

Hans Troost

Word Heretic said:
G'day (e-mail address removed) (Hans Troost),

A neat Easter Egg that isnt worth removing :) I think it might be in
the Enter key code processing part as if you intercept the enter event
you disable lots of magic stuff. Hmmm.
Thanks, but I'm afraid that I don't understand what you exactly mean.
Perhaps because I'm Dutch or do not understand english good enough??.

Did you suggest a solution and if Yes which one? Or did you tell me a
reason (which one) that it will never work...

Groeten / Regards

Hans
 
J

Jonathan West

Hi hans,

I've done a bit of experimenting, and it looks like you cant use =rand in
code. I suggest you greate an autotext entry with the sentence and insert
that from code instead.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
D

Doug Robbins - Word MVP

Sorry Jonathon,

Just to prove you wrong,

SendKeys "=rand{(}{)}~"

works.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jonathan West

Doug Robbins - Word MVP said:
Sorry Jonathon,

Just to prove you wrong,

SendKeys "=rand{(}{)}~"

works.

LOL! Nice one! I hadn't thought of using Sendkeys...

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top