none
How can I find the position of a Bookmark within a Word Document Range? RRS feed

  • Question

  • I am trying to determine where the body of a piece of correspondence starts and ends - be it an E-mail, Fax or Letter - cutting off any header and footer. For out-going correspondence we've made this easy by placing 'BodyStart' and 'BodyEnd' Bookmarks in our Templates.

    For in-coming E-mails things are a little more complicated, as it may contain an out-going E-mail earlier in the thread, complete with 'BodyStart' and 'BodyEnd' Bookmarks. So we need to check that any 'BodyStart' Bookmark is within the first few rows of the E-mail.  But here's the rub.   If we use WordDocument.Bookmarks("BodyStart").Start we can find the location of the Bookmark without problems.  But if we then use Set Range_DocumentBody = WordDocument.Range to get access to the text within WordDocument, we find that the location of the Bookmark is in a somewhat different position in the Range from the original Document. 

    We HAVE resolved the problem by creating another Range to identify the first 32 Characters following the 'BodyStart' Bookmark. We then can find their position within Range_DocumentBody and count the number of Carriage Returns before the Bookmark.  However there must be a more elegant way of finding the position of a Bookmark within a Word Document Range.

    (If the 'BodyStart' Bookmark is too far into the E-mail, then we have a Function that reads the correspondence looking for all the ways correspondence can start and end to extract the body.   So THAT is not a problem.)

    Any ideas anyone?

    Thursday, May 5, 2016 3:25 AM

Answers

  • In that case, you need to check whether the bookmark exits. Having established that it does, you can then contract the range and use something such as a paragraph count for finding whether it's in the first few 'lines':

    Set Range_DocumentBody = WordDocument.Range
    If WordDocument.Bookmarks.Exists("BodyStart") Then
        Range_DocumentBody.End = WordDocument.Bookmarks("BodyStart").Range.Start
    End If
    MsgBox Range_DocumentBody.Paragraphs.Count


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, May 5, 2016 9:22 AM

All replies

  • I don't quite follow what you mean about ranges changing, but why not set the Range_DocumentBody first, then find the 'BodyStart' bookmark within that. For example, if you want to capture the content before that bookmark:

    Set Range_DocumentBody = WordDocument.Range
    With Range_DocumentBody
      .End = .Bookmarks("BodyStart").Range.Start
      MsgBox .Text
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, May 5, 2016 8:12 AM
  • I mean that the value returned by WordDocument.Bookmarks("BodyStart").Start is NOT the Character position of the Bookmark in Range_DocumentBody.  I am not trying to return the Text in the Range.  I am trying to validate whether the Bookmark is within the first few rows of the E-mail.  If it is, I can return the contents of the Range as you suggest.  If it isn't then I need to read the text in the E-mail looking for known Salutations and YoursPhrases to determine where the most recent E-mail in the thread starts and finishes.
    Thursday, May 5, 2016 8:47 AM
  • In that case, you need to check whether the bookmark exits. Having established that it does, you can then contract the range and use something such as a paragraph count for finding whether it's in the first few 'lines':

    Set Range_DocumentBody = WordDocument.Range
    If WordDocument.Bookmarks.Exists("BodyStart") Then
        Range_DocumentBody.End = WordDocument.Bookmarks("BodyStart").Range.Start
    End If
    MsgBox Range_DocumentBody.Paragraphs.Count


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, May 5, 2016 9:22 AM
  • THAT sounds like a good idea.  It does not solve the problem as originally stated - BUT it does solve my REAL problem!  Many thanks, I'll let you know how I get on.
    Thursday, May 5, 2016 10:45 PM
  • Hi Brian,

    Have your issue been resoled? If you have, I suggest you mark helpful reply as answer to close this thread. If not, please feel free to let us know your current issue.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 9, 2016 7:09 AM