How to create a random sentence in word using VBA?
For example the code beneath created a sentence the cat sat on the mat1. I would like to declare words in place of i.
Is it possible using VBA?
Sub Randomsentence()
Dim text As String
Dim s As String
MyText = "The cat sat on the"
i = Int(4 * Rnd())
Selection.TypeText (MyText)
Selection.TypeText (i)
End Sub
The following declafres an array and fills it with words. Then a random word is selected from the array and added to the sentence (shown as MsgBox for simplicity):
Sub Randomsentence()
Dim MyText As String
Dim s(5) As String
Dim i As Integer
s(1) = "mat"
s(2) = "floor"
s(3) = "roof"
s(4) = "car"
s(5) = "garage"
MyText = "The cat sat on the "
i = Int(5 * Rnd())
MsgBox MyText & s(i)
End Sub
A maybe nicer way to do that is to read the words from a file. I leave that to you as a nice excercise.