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 / Mailmerge and Fax / February 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Charles Kenyon - 25 Feb 2004 00:59 GMT
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
Signature


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.

Peter Hewett - 25 Feb 2004 03:21 GMT
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

> 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
[quoted text clipped - 85 lines]
>
> TIA
Peter Hewett - 25 Feb 2004 03:31 GMT
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

> Hi Charles
>
[quoted text clipped - 19 lines]
>
> HTH + Cheers - Peter
Charles Kenyon - 25 Feb 2004 16:01 GMT
Thank you for both notes. You solved another mystery for me.

Signature

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.

> Hi Charles
>
[quoted text clipped - 29 lines]
> >
> > HTH + Cheers - Peter
Charles Kenyon - 25 Feb 2004 16:17 GMT
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.
Signature


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.

> Hi Charles
>
[quoted text clipped - 109 lines]
> >
> > TIA
 
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.