AutoText entry with merge codes stripped of those codes when inserted via vba

C

Charles Kenyon

I have a complex problem using Word 2003. It may be in other versions, just
haven't tested. It involves a UserForm that interposes itself between two
merge dialogs when a new document is created.

I've got a discovery demand that I routinely use. It is a merge template
merging with an Excel spreadsheet. Creation of a new document based on the
template starts with a UserForm that allows modification for three different
types of cases. In one of those cases (DUI) the Cmd_OK_Click handler inserts
a number of AutoText entries at various locations in the document (noted by
bookmarks). This works fine for all of the entries except one that has merge
codes in it.

Those merge codes, themselves, include an IF field to test for a middle
initial.

This includes any videotape made of {MERGEFIELD Client_First }{MERGEFIELD
Client_MI }{ IF {MERGEFIELD Client_MI }= "" "" " " }{ MERGEFIELD
Client_Last } whether or not the government considers said video to be a
"statement."

The resulting text should be:

This includes any videotape made of First M. Last whether or not the
government considers said video to be a "statement."

or

This includes any videotape made of First Last whether or not the
government considers said video to be a "statement."

If I insert the AutoText entry manually, the codes show up and work with no
problem. But when I rely on the procedure I get the following instead:

This includes any videotape made of * MERGEFIELD Client_First *First* *
MERGEFIELD Client_MI *M.** IF * MERGEFIELD Client_MI *M.* = "" "" " " * **
MERGEFIELD Client_Last *Last* whether or not the government considers said
video to be a "statement."

None of this is fields, it is all hard text.

The code to insert works with all of the other AT items inserted (none of
which, I believe, contain merge codes). If I run a macro to insert the AT
entry in question in the existing document, it works fine.

To make it even quirkier, I have run it once (out of about 40 times) where
it properly inserted the fields. That time was just after I had run the
individual macro (in a different document) inserting the AT entry. I then
created a new document and it worked. Haven't been able to replicate it
working.

The relevant portion of the code inserting the AutoText is:

Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("BmDxDWI2").Range
BMRange.Text =
ActiveDocument.AttachedTemplate.AutoTextEntries("AtDxDWI2")
BMRange.Select
ActiveDocument.Bookmarks.Add "BmDxDWI2", BMRange

A recorded macro inserting the same AutoText entry at the selection point
works. It is:
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 2/24/2004 by Charles Kyle Kenyon
'
Selection.GoTo What:=wdGoToBookmark, Name:="BmDxDWI2"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Application.DisplayAutoCompleteTips = True
ActiveDocument.AttachedTemplate.AutoTextEntries("AtDxDWI2").Insert
Where:= _
Selection.Range, RichText:=True
Selection.MoveUp Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="BmDxDWI2"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.HomeKey Unit:=wdLine
End Sub

Sorry for being so long and complex.

TIA
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
P

Peter Hewett

Hi Charles

It's the way you're inserting the AutoText entry. This is the code I always
use:

Public Sub InsertAutoText(ByVal strAutoTextName As String, _
ByVal strBookmarkName As String)
Dim tplAttached As Word.Template
Dim rngLocation As Word.Range

' Insert specified autotext at the bookmarked location
' and then recreate the bookmark
Set tplAttached = ActiveDocument.AttachedTemplate
Set rngLocation = ActiveDocument.Bookmarks(strBookmarkName).Range
Set rngLocation = tplAttached. _
AutoTextEntries(strAutoTextName).Insert(rngLocation, True)
ActiveDocument.Bookmarks.Add strBookmarkName, rngLocation
End Sub

The way you're doing it you just get the textural version of the autotext.
I.e. no rich text component, no fields etc.

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Charles

I forgot to add, if memory serves me right inserting the autotext like this:
BMRange.Text = _
ActiveDocument.AttachedTemplate.AutoTextEntries("AtDxDWI2")

only inserts the first 255 characters of the AutoText entry.

Cheers - Peter
 
C

Charles Kenyon

Thank you for both notes. You solved another mystery for me.


--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charles Kenyon

Thank you, again.

I ended up adding the following to the code to stop a prompt on saving the
template when closing the result document.

tplAttached.Saved = True

I'm not sure where the modification came in, but I got the prompt with your
code.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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