Results 1 to 4 of 4

Thread: Field as range

  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Is there a way to assign a Word field as a range? I can do this by using the select method to select the field, and then create the range with Set rngField = selection.range.

    For example, I'd like to apply bold and underline to all fields in my document. This would be for review purposes, making it clear in the printed document what is regular text and what is a field.

    I use the following, which works well. I would prefer to not use the selection object, using a range object for each field instead, and gaining a speed benefit. Is thee a way to do that?

    Dim f As Field
    Dim x
    Dim rng As Range
    For x = 1 To ActiveDocument.Fields.Count
    ActiveDocument.Fields(x).Select
    Set rng = Selection.Range
    Selection.Collapse
    rng.Bold = True
    rng.Underline = wdUnderlineThick
    Next x

    Ideally, I'd like to use set rng = ActiveDocument.Fields(x), but that is not an option.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    The following code should work (pretty quickly):

    Code:
    Sub Bold_UL_ToAllFieldsInDoc()
       Dim aField As Field
       For Each aField In ActiveDocument.Fields
          aField.Result.Bold = True
          aField.Result.Font.Underline = wdUnderlineSingle
       Next 'aField
    End Sub
    Notes:
    No need to set the range, nor select the field. Fields don't return a range property, but you can change the font properties of Field.Result
    Using a For Each loop purportedly runs up to 100 times faster than a 'For x = 1" loop.

    Gary

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Thank you very much. I see that I can add text immediately after a field using f.Result.InsertAfter. That will be helpful, too.

    I typically do use ForEach for something like this, but the Select method (which I now won't need to use) didn't like that.

    Thanks again.
    Richard

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    To apply bold formatting to a field, adding a Charformat switch (and bolding the first letter of the field code) is likely to produce more reliable results. All too often I've seen cases where a REF field has been bolded and, when the source text is lengthened, the additional text captured by the REF field doesn't get bolded.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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