Trying to automate populating a Word doc with random text using the "=rand(x,y)" function

S

Stephen D. Oliver

I'm sure everyone knows about how you can type "=rand(x,y)" (minus the
quotation marks) into a Word doc and have it automatically populate the doc
with "dummy text." (It works in Word 2003 and Word 2007 but uses different
dummy text for each version.)
Well, I had the grand idea of automating that by using a Word VBA macro that
typed the function directly on to the document surface and then inserted a
carriage return.
I'm a bit baffled as to why, but for some reason that doesn't work. I use
this code:

Sub AddDummyText()
Selection.TypeText ("=rand(2,4)")
SendKeys "{ENTER}", True

End Sub

When you run that code, the insertion point stays just behind the last
parens and basically nothing happens. I tried this as well:

Sub AddText()
Selection.TypeText ("=rand(2,4)")
Selection.InsertParagraph

End Sub

That didn't work either. The frustrating part is you can switch to the doc
and then just hit Enter and it works as advertised.

Has anyone run into this before?

-so
 
S

Summer

Me thinks you are working way to hard.

If you want dummy text lorem ipsum or rand ... then put a dummy set of
paragraphs on a blank page. Highlight the dummy text 3 or 4 paragraphs and
go to Tools Autocorrect and save it as
d/
Yes that is it 2 characters.

Now type: d/
on your screen and if you want a further 4 paragraphs (8 in all) type d/
again,

I think this is quicker. You may also use autotext to achieve the same or
quickparts and/or autotext.

Hope this assists
 
S

Stephen D. Oliver

that would be great except i want it entirely automated.
to be honest, i'm trying to validate it in VB before I try it in something
like VSTO.
 
S

Summer

If I miss the point again you would use quickparts and autotext (compatible
2003/2007) and insert autotext in the vba:

Sub Macro1()
'
' Macro1 Macro
'
'
NormalTemplate.BuildingBlockEntries("dummytext").Insert
Where:=Selection.Range, _
RichText:=True
NormalTemplate.BuildingBlockEntries("dummytext").Insert
Where:=Selection.Range, _
RichText:=True
End Sub

Perhaps you should be in the VBA group?

Hope this helps.
 
S

Stephen D. Oliver

I'll probably have to try something along those lines.
I'm not sure what the VBA group is?
I thought that this was a VBA newsgroup, no?

-so
 
S

Summer

You are quite correct - I thought it was document management - busy day.

You should get some more responses that will assist I am sure.

Rand instruction is not type text as in "the dog sat on mat." so that would
never work.
 
G

Graham Mayor

It's a quiet day ;) so

Dim sRandomText As String
Dim iSent As Integer
Dim iPara As Integer
Dim sInput As String
Dim x As Long
Dim y As Long
sRandomText = "The quick brown fox jumps over the lazy dog. "
start:
sInput = InputBox("Enter numbers of paragraphs and sentences", _
"Random Text", "2,4")
If Len(sInput) <> 3 Then
MsgBox "Two many digits - enter up to 9 paragraphs and sentences", _
vbCritical, "Error"
GoTo start:
End If
iSent = Right(sInput, 1)
iPara = Left(sInput, 1)
For x = 1 To iPara
For y = 1 To iSent
Selection.TypeText Text:=sRandomText
Next y
Selection.TypeParagraph
Next x

should give you something to play with :)
Autotext or autocorrect are simpler!

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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