none
Empty highlight found RRS feed

  • Question

  • We have the usual VBA code to look for the next highlighted item in our Word document, followed by

      if selection.find.found then

        if asc(selection.range.text) = 65 then    'to see if the first character in the highlight is an A

    However, selection.find.found evaluates to true but selection.range.text is an empty string, so asc(selection.range.text) throws an error.

    Question:

    How can VBA find a highlight which is empty. I don't see how something can be highlighted AND empty. We have checked to make sure that it is not a comment or a field being found. Anyone any ideas.

      

    Thursday, June 28, 2012 3:12 PM

Answers

  • You could avoid the error by using

    If Selection.Type <> wdSelectionIP Then
        If Asc(Selection.Range.Text) = 65 Then


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Saturday, June 30, 2012 10:17 AM
  • ..or...

    try putting

    Selection.Collapse

    at the start of your instructions actioning the Selection.  This is the mirror to Doug's

    If Selection.Type <> wdSelectionIP

    wdSelectionIP is the property the describes the state of the Selection- whether it is collapsed to ONLY the Insertion Point (the IP)...or not.  Thus

    If Selection.Type <> is not collapsed to an Insertion Point

    So, collapse it first.  Although I am wondering about your use of the word "highlight".  This may be an issue if what you are talking about is doing a Find on what you have selected (highlighted).


    Word MVP


    Wednesday, July 4, 2012 5:07 AM

All replies

  • You could avoid the error by using

    If Selection.Type <> wdSelectionIP Then
        If Asc(Selection.Range.Text) = 65 Then


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Saturday, June 30, 2012 10:17 AM
  • ..or...

    try putting

    Selection.Collapse

    at the start of your instructions actioning the Selection.  This is the mirror to Doug's

    If Selection.Type <> wdSelectionIP

    wdSelectionIP is the property the describes the state of the Selection- whether it is collapsed to ONLY the Insertion Point (the IP)...or not.  Thus

    If Selection.Type <> is not collapsed to an Insertion Point

    So, collapse it first.  Although I am wondering about your use of the word "highlight".  This may be an issue if what you are talking about is doing a Find on what you have selected (highlighted).


    Word MVP


    Wednesday, July 4, 2012 5:07 AM