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 / September 2006

Tip: Looking for answers? Try searching our database.

Autotext

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jen - 23 Sep 2006 16:40 GMT
Hi all

Randomizer code below selects Autoetxt entries randomly and inserts at end
of active document.

My question is - is it possible to alter the below code to select Multiple
Autotext Entries by entering the Autotext entry's
name(s), for example:

   Question2
   Question3
   Question10

selecting from Attached Template.

Can those autotext entries load into an array?

Then have the code insert the selected Autoext entries in order of selection
into active document, preferably at current cursor position not at end of
document?

Thanks for ANY help.

> 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
Tony Jollans - 24 Sep 2006 09:30 GMT
This seems to be a better worded version of your earlier question.

You seem to be asking if code can be written to rack up a load of autotexts
and enter them all at once. Of course it can but what purpose would it
serve? It would be quicker, easier, and more accurate to use the standard
Word interface.

I haven't looked at your code but I'm sure most of it is related to the
randomising rather than the autotexts themselves and I'm sure it could be
easily altered but can you give a bit more detail as to what you really
want?

--
Enjoy,
Tony

> Hi all
>
[quoted text clipped - 67 lines]
> >    Next
> > End Sub
Jen - 24 Sep 2006 03:35 GMT
Hi Tony
Thanking everyone for their patience.

I have 150 autotext entries which could be used to create the content of
each document. Authors will dictate the autotext entries required, for
example:

Question1
Question3
Question10
Question2

I need to provide an input box for the user to type the question numbers: 1,
3, 10, 2.

And then insert the corresponding autoext from attached template.

I have only done boilerplate with two listboxes where users can add and
remove selected clauses from liist box and then the documents are inserted
into active document. I have now converted these documents to autotext.

Or can I use the two listbox method - on left are selections and on right
are the selections to be added to document?  I will try that. I am not sure
if this is easiest way to insert autotext entries.

And am still unaware of how to insert multiple autoext entries with a loop.

Hope this is a little clearer. Not too experienced with VBA.

Thank you

> This seems to be a better worded version of your earlier question.
>
[quoted text clipped - 88 lines]
>> >    Next
>> > End Sub
Tony Jollans - 24 Sep 2006 16:09 GMT
And what is the problem with using Insert > AutoText > AutoText?

You could write a macro which prompted for autotext IDs but it would have to
validate them all in case of typos. To run it you would have to start it up
somehow (maybe off a custom toolbar) and type in the names one by one.
Alternatively you could start up the built in list (maybe off the built in
toolbar) and just select the ones you wanted. I really don't see why you
need a macro but something like this should get you started ...

   Dim ATNames() As String
   ReDim ATNames(0)

   Do
       ATNames(UBound(ATNames)) = InputBox("Next")
       If ATNames(UBound(ATNames)) = "" Then
           Exit Do
       Else
           ReDim Preserve ATNames(UBound(ATNames) + 1)
       End If
   Loop

   On Error Resume Next
   For ndx = LBound(ATNames) To UBound(ATNames)
       ActiveDocument.AttachedTemplate.AutoTextEntries(ATNames(ndx)).Insert
Where:=Selection.Range
   Next

Note that this simply ignores errors which might not be what you want.

--
Enjoy,
Tony

> Hi Tony
> Thanking everyone for their patience.
[quoted text clipped - 119 lines]
> >> >    Next
> >> > End Sub
Fred - 25 Sep 2006 15:09 GMT
Tony,

Autotext is used. I've also used it off the dropdown - found it loses
connection to template if template is renamed or moved during network
changes (updates and so on - IT ). Users get slightly put off with the
dropdown when it doesn't work because it cannot locate the Autotext entry,
but it was a good intro to get them using autotext.

Thank you for helping me out with this.

Like the email address (just noticed it).

> And what is the problem with using Insert > AutoText > AutoText?
>
[quoted text clipped - 163 lines]
>> >> >    Next
>> >> > End Sub
Fred - 25 Sep 2006 15:43 GMT
Tony

Just set up a new computer - so reply name is different.

> Tony,
>
[quoted text clipped - 183 lines]
>>> >> >    Next
>>> >> > End Sub
Tony Jollans - 25 Sep 2006 17:12 GMT
Hi Fred,

Now I'm confused. If you rename or move the template, it won't be
available - unless you take specific action - regardless of how you want to
use it. Neither Word nor any macro you write is psychic.

--
Enjoy,
Tony

> Tony,
>
[quoted text clipped - 175 lines]
> >> >> >    Next
> >> >> > 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.