Results 1 to 4 of 4
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Range position after Autotext entry (Word 2002 SP-2)

    Yeesh! Working with Ranges can certainly discourage a guy from working with Ranges.

    If I want to insert an Autotext entry using the Selection object and have the Selection end up as an IP immediately following the Autotext entry, it's this simple:

    NormalTemplate.AutoTextEntries("Filename and path").Insert _
    Where:=Selection.Range

    If I want to insert an Autotext entry using the Range object (at the location of a Range variable -- oRng -- that's an IP) and have oRng end up as an IP immediately following the Autotext entry (instead of before the entry, where it initially ends up), here are the hoops I'm currently jumping through:

    Dim rngOrig As Word.Range
    Set rngOrig = oRng.Duplicate
    rngOrig.InsertParagraph
    NormalTemplate.AutoTextEntries("Filename and path").Insert _
    Where:=oRng
    rngOrig.Characters.Last.Delete
    Set oRng = rngOrig.Duplicate
    oRng.Collapse wdCollapseEnd
    Set rngOrig = Nothing

    Is there a simpler way?

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Range position after Autotext entry (Word 2002 SP-2)

    <P ID="edit" class=small>(Edited by Andrew77 on 26-Aug-04 21:17. fixed minor error (needed some parens))</P>There is a simpler way.

    The Insert method of the AutoTextEntry object returns a range. So try this instead:

    <pre>Set oRng = NormalTemplate.AutoTextEntries("Filename and path").Insert(Where:=oRng)
    oRng.Collapse wdCollapseEnd
    </pre>


    HTH

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Range position after Autotext entry (Word 2002

    <P ID="edit" class=small>(Edited by jscher2000 on 26-Aug-04 14:19. Duh. Andrew's way is way more efficient! Save the math for a case where you can't get a specific object reference.)</P>Here's a more mathematical approach that gets even further away from the idea of selections and bookmarks:
    <pre>Sub TestRangeMath()
    Dim rngTemp As Range, lngDocLength As Long
    ' Set initial range for testing
    Set rngTemp = Selection.Range
    ' Capture current character count
    lngDocLength = rngTemp.Document.Characters.Count
    ' Insert AutoText entry
    NormalTemplate.AutoTextEntries("thx").Insert _
    where:=rngTemp, RichText:=False
    ' Expand rngTemp to end of AutoText entry
    rngTemp.MoveEnd unit:=wdCharacter, _
    Count:=rngTemp.Document.Characters.Count - lngDocLength
    ' For testing, select it to make sure it worked
    'rngTemp.Select
    'Stop
    ' Collapse rngTemp to its end
    rngTemp.Collapse wdCollapseEnd
    ' For testing, select it to make sure it worked
    'rngTemp.Select
    'Stop
    ' Clean up objects
    Set rngTemp = Nothing
    End Sub</pre>

    That works in the document body, but I have this sneaking suspicion that you want code that works in a header or footer. In that case, you would need to substitute the appropriate Header or Footer range for rngTemp.Document (can be a bit tricky, but perhaps you already have the reference somewhere in your code).

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Range position after Autotext entry (Word 2002 SP-2)

    A massive improvement. Much obliged. <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

    I often find myself coming up with range-based code and thinking there must be a simpler way. Can anyone recommend a good internet source or two for Word VBA samples, particularly ones that work with ranges? (I'm guessing Jefferson might want to take a peek as well. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •