Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    Hi, all. Again, I'm humbled... after all these years, I do not know the most efficient way of doing something that seems so simple.

    I want to programmatically insert text immediately preceding a field code, using the Range object. I figured I'd determine the "insertion point" by setting a range variable to the same range as the current Field.Result, then collapsing it. But when I do that and use the Insert method, the text is added to the field results, which of course means that it will go away when the field is next updated. I want it added outside the field, in front of it.

    So, my question is, what's the trick to adding text to a range immediately preceding a field code? Should I not use the field code's range when I initially use the Set statement for the insertion range? Is there an obscure argument to the InsertAfter method that prevents the text from becoming part of the following field?

    Here is a snippit of how I'm currently trying to do it. (The tField variable is the current field, which is part of the document's Fields collection, accessed with a "For Each" loop.)

    Set nRange = tField.Result
    nRange.Collapse
    nRange.InsertAfter ": "

    Any tips? Tricks? Best Practices?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    Not sure about best practices, but this should work:<pre>myRange.MoveStartUntil CSet:=Chr(19), Count:=wdBackward
    myRange.MoveStart Unit:=wdCharacter, Count:=-1
    myRange.Collapse (wdCollapseStart)</pre>

    It moves the cursor to the left until it finds the code of an opening field brace ^19, then one character more.
    I imagine it's hard to impossible to do using only Field.Code or Field.Result, since some types of fields have (or can have) one of those, or the other, or both.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  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: Inserting text immediately preceeding a Field

    Along the lines suggested by Klaus, you can move the start of the range back one character before collapsing, then advance it one character, to avoid adding content to the field:

    <code>nRange.MoveStart wdCharacter, -1
    nRange.Collapse wdCollapseStart
    nRange.MoveStart wdCharacter, 1</code>

    Obviously this won't work if the field is the first thing in the document, but I suppose that doesn't happen very often...

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    And piling on, this one does seem to work regardless of whether the field is the first thing in the document or not.
    <pre> Dim nRange As Range
    Set nRange = ActiveDocument.Fields(1).Code
    nRange.Start = nRange.Start - 1
    nRange.InsertBefore ": "</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    I neglected to thank you for your help (and, since I have been helped by you in the past, that is most egregious... Sorry!)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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