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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    Author
    Topic
    #441429

    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?

    Viewing 2 reply threads
    Author
    Replies
    • #1060031

      Not sure about best practices, but this should work:

      myRange.MoveStartUntil CSet:=Chr(19), Count:=wdBackward
      myRange.MoveStart Unit:=wdCharacter, Count:=-1
      myRange.Collapse (wdCollapseStart)

      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.

      cheers Klaus

    • #1060035

      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:

      nRange.MoveStart wdCharacter, -1
      nRange.Collapse wdCollapseStart
      nRange.MoveStart wdCharacter, 1

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

    • #1060036

      And piling on, this one does seem to work regardless of whether the field is the first thing in the document or not.

        Dim nRange As Range
        Set nRange = ActiveDocument.Fields(1).Code
        nRange.Start = nRange.Start - 1
        nRange.InsertBefore ": "
      • #1060612

        I neglected to thank you for your help (and, since I have been helped by you in the past, that is most egregious… Sorry!)

    Viewing 2 reply threads
    Reply To: Inserting text immediately preceeding a Field (Office XP/2003 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: