Autotext - help with Multiple inserts

J

Jen

Hi

I have VBA routine that automatically inserts and Randomises ATe from
attached template (document) and you insert number for Integer 1 2 50 etc.
And it tosses in some random ATe entries. Great educationally.

I would also like to be able to select A1 A2 A3 A4 A5 A6 or B1 B2 or even 1
5 15 3 6 2 from attached templated or named Global Template and have the
multientries insert at current cursor position.

Can anyone help me to achieve this? The randomiser is great for tests.

Would like some help altering randomiser code to insert entries in selected
input order at current cursor position?

Option Explicit

Public Sub InsertRandomAutoText()
Const cNoOfATRequired As Integer = 2
Dim rngLocation As Word.Range
Dim tplAttached As Word.Template
Dim lngRandMax As Long
Dim lngIndex As Long
Dim lngSwapWith As Long
Dim lngTemp As Long
Dim alngNumbers() As Long
' Highest random number that we need to generate
Set tplAttached = ActiveDocument.AttachedTemplate
lngRandMax = tplAttached.AutoTextEntries.Count
If lngRandMax < cNoOfATRequired Then
MsgBox "There are insufficient AutoText entries in the" & _
vbCr & "attached template - at least " & cNoOfATRequired & " are
required"
Exit Sub
End If
' The starting point is a list of sequential
' numbers, one for each AutoText entry
ReDim alngNumbers(1 To lngRandMax)
For lngIndex = 1 To lngRandMax
alngNumbers(lngIndex) = lngIndex
Next
' Seed random number generator
Randomize
' Now randomise the list we've just generated
For lngIndex = 1 To lngRandMax
lngSwapWith = Int(lngRandMax * Rnd + 1)
' Swap a randomly selected entry with the index entry
lngTemp = alngNumbers(lngIndex)
alngNumbers(lngIndex) = alngNumbers(lngSwapWith)
alngNumbers(lngSwapWith) = lngTemp
Next
' Now insert the first cNoOfATRequired autotext entries
' at the end of the document
Set rngLocation = ActiveDocument.Content
rngLocation.Collapse wdCollapseEnd
For lngIndex = 1 To cNoOfATRequired
' Insert a random AutoText entry
Set rngLocation = _
tplAttached.AutoTextEntries(alngNumbers(lngIndex)).Insert _
(rngLocation, True)
rngLocation.Collapse wdCollapseEnd
Next
End Sub
 
J

Jezebel

It would be easier to help if your question were intelligible. Perhaps you
could get an English speaker to help you with the translation and post
again.
 

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