Bug: Word loops when inserting AutoText from a Template file

E

Erik G

I get a loop in Word (100% cpu) when I try to do the following:
1. Open a template for edit
2. In the template i have a table with autotexts.
3. I run a macro to read autotext name and contents from the table.
4. The autotext contents has new lines in it. (More than one paragraph. If
it is just a sentence, everything works fine).

If I open the template as a new document everything works fine. I have done
a workaround in the macro using a temporary document where a paste the
contents and define the range.

The macro:

Sub ReloadAutoText()
Dim tbl As Table
Dim myRow As row
Dim myTemplate As Template
Dim myRange As Range

' Remove all autotextentries
Set myTemplate = ActiveDocument.AttachedTemplate
For i = myTemplate.AutoTextEntries.Count To 1 Step -1
myTemplate.AutoTextEntries(i).Delete
Next

' Add autotext entries from table
Set tbl = ActiveDocument.Tables(1)
' Due to bug use temp document to paste autotext contents
If Documents.Count = 1 Then Documents.Add
For i = 1 To tbl.Rows.Count
Set myRow = tbl.Rows(i)

' Get name for autotext
Set myRange = myRow.Cells(1).Range
myRange.Collapse wdCollapseStart
myRange.Expand wdCell
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
MyName = myRange.Text

' Get value of autotext
Set myRange = myRow.Cells(2).Range
myRange.Collapse wdCollapseStart
myRange.Expand wdCell
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
myRange.Select

' Workaround code
Selection.Copy
Documents(1).Activate
Selection.Range.Collapse wdCollapseStart
Selection.Paste
PasteLen = Len(myRange.Text)
Set myRange = ActiveDocument.Range
myRange.Collapse wdCollapseStart
myRange.MoveEnd Unit:=wdCharacter, Count:=PasteLen
myRange.Select
' Add autotext entry
myTemplate.AutoTextEntries.Add Name:=MyName, Range:=myRange
ActiveDocument.Select
Selection.Delete
Next
' Drop temp document
Documents(1).Activate
ActiveDocument.Close SaveChanges:=False

End Sub

I get the same error in Word2002 SP1 and Word2003.
 

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