Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting Ranges (Office 2003)

    I'm sure this is a simple answer but for the life of me I just can't get it to work !!

    I have code which identifies a bookmark, sets the bookmark as a range and places a filepath (field) in the bookmark. I then want to wrap a new bookmark, with the same name as before, around the field - this works well with other ranges etc but doesn't seem to work with the field object. My code takes me as far as placing the field in the range but can't seem to wrap the bookmark around the field.

    Set rngBookmark = docDocument.Bookmarks.Item(strBookmark).Range
    Set fldField = docDocument.Fields.Add(rngBookmark, wdFieldEmpty, "FILENAME p ", PreserveFormatting:=True)
    docDocument.Bookmarks.Add strBookmark, rngBookmark

    I'm sure this is a simple fix and I just need to manipulate the range but am getting a little confused. When the code runs the bookmark is placed at the beginning of the field rather than around the field. I did try to do a field count in the document after the filepath had been inserted but no fields can be found !!!

    Any help would be greatly appreciated.
    Thanks
    Jack

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Setting Ranges (Office 2003)

    Inserting the field removes the bookmark, so the range is collapsed to zero width. You could insert these two lines before adding the bookmark again:

    fldField.Select
    Set rngBookmark = Selection.Range

  3. #3
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Setting Ranges (Office 2003)

    Many thanks Hans, it works a treat.

    Regards
    Jack

  4. #4
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Setting Ranges (Office 2003)

    Hans

    I spoke too soon !! Is there a way I can set the Field as a Range rather than physically select it. At the moment the field is held in the first page footer so by using fldField.Select the footer is opened on the page as it is selected and stays open.

    Many thanks
    Jackie

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Setting Ranges (Office 2003)

    I'm afraid I don't know how to determine the range associated with a field. Fields don't seem to have a Range property. That's why I used Select. Perhaps somebody else knows how to avoid Select. Otherwise, you can check if you're in the footer and close it if necessary.

  6. #6
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Setting Ranges (Office 2003)

    I've worked around the Select issue. At the beginning of the code I have stored the page view option, I then run the code, close the Footer and take the user back to the page view option they originally started with. (When manipulating the headers/footer the page view goes to Normal, hence I have put the additional code in.)

    Many thanks for your help.
    Regards
    Jack

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

    Re: Setting Ranges (Office 2003)

    Hi Hans,

    It's quite annoying that there's no range property for fields. Here's a couple workarounds I've found handy:

    To get the range of an entire field, including the field braces:

    <pre>Dim rng As Range
    Dim fld As Field
    Set fld = ActiveDocument.Fields(1)
    Set rng = fld.Parent.Range(fld.Code.Start -1, fld.Code.End + 1)
    </pre>


    To get the range of just the field code, without the braces:
    <pre>Dim rng As Range
    Dim fld As Field
    Set fld = ActiveDocument.Fields(1)
    Set rng = fld.Parent.Range(fld.Code.Start, fld.Code.End)
    </pre>


    And to get the range of the field result:
    <pre>Dim rng As Range
    Dim fld As Field
    Set fld = ActiveDocument.Fields(1)
    Set rng = fld.Parent.Range(fld.Result.Start, fld.Result.End)
    </pre>


    Perhaps you (and others) will find this helpful.

    Cheers!

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Setting Ranges (Office 2003)

    Thanks, that'll come in handy. I'll store it in my bag of tricks.

Posting Permissions

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