Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen D. Oliver - 13 Nov 2007 06:48 GMT
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
Summer - 13 Nov 2007 06:59 GMT
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

> 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
[quoted text clipped - 27 lines]
>
> -so
Stephen D. Oliver - 13 Nov 2007 07:03 GMT
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.

> Me thinks you are working way to hard.
>
[quoted text clipped - 44 lines]
>>
>> -so
Summer - 13 Nov 2007 07:10 GMT
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.

> 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
[quoted text clipped - 48 lines]
>>>
>>> -so
Stephen D. Oliver - 13 Nov 2007 07:17 GMT
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

> If I miss the point again you would use quickparts and autotext
> (compatible 2003/2007) and insert autotext in the vba:
[quoted text clipped - 68 lines]
>>>>
>>>> -so
Summer - 13 Nov 2007 07:21 GMT
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.

> I'll probably have to try something along those lines.
> I'm not sure what the VBA group is?
[quoted text clipped - 75 lines]
>>>>>
>>>>> -so
Graham Mayor - 13 Nov 2007 08:47 GMT
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!

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> You are quite correct - I thought it was document management - busy
> day.
[quoted text clipped - 82 lines]
>>>>>>
>>>>>> -so
DuncanL - 13 Nov 2007 11:56 GMT
The following works for me:

Sub AddText()
   SendKeys "=rand{(}2,4{)}{ENTER}"
End Sub
Summer - 13 Nov 2007 13:01 GMT
Yes the ENTER key is the key.
> The following works for me:
>
> Sub AddText()
>    SendKeys "=rand{(}2,4{)}{ENTER}"
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.