Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you evaluate what's at the insertion point?

    I'm writing a macro in which I need to evaluate what's at the insertion point. There are two different things I need to do:

    1. The document I'm working with has a number of sections. I need to check whether the Section Break is preceded by one or more unneeded paragraph makers. If so, I want to delete the paragraph markers.

    2. The macro selects text in a table and assigns it to a variable. I need to evaluate if the selection is empty (as is the case if the cell is blank). Although it appears on the screen that nothing is selected, testing it against an empty string ("") does not test as true. When I displayed the results of the variable in a list box on a user form, the variable seems to have two codes in it--a paragraph marker and something else that displays as an empty square. I can't figure out how to test for this condition.

    I seem to be missing a concept in regard to evaluating what's at the insertion point. In the WordPerfect macro language, you could evaluate what's at the right or left of the insertion point with all sorts of built-in commands/codes, and I don't know how to do the equivalent in Word VBA.

    Your help will be greatly appreciated. Thanks!

    - Linda

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

    Re: How do you evaluate what's at the insertion point?

    1. Look up the MoveLeft and MoveRight methods in the Word VBA help.
    2. An empty cell contains the characters Chr(13) (end of paragraph character) and Chr(7) (end of cell character). So you can test if the selection is equal to Chr(13) & Chr(7).

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How do you evaluate what's at the insertion po

    I assume you are comparing using Selection.Text. As you have noticed, if the selection is just an insertion point, Selection.Text returns the next "character" and, as Hans describes, when the character is an "end-of-cell" marker, you get two control codes. If you use a Select Case structure, you can rather neatly handle both the strange and ordinary results you might get for Selection.Text.

    Now, if you really need to look to the left of the insertion point, you could consider selecting one character to the left. In that case, Selection.Text will report the selected character. Doing this in a table may lead to peculiar results, so in that context, I definition would not recommend it!

  4. #4
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do you evaluate what's at the insertion po

    Thanks for your help! Chr(13) & Chr(7) worked perfectly for me to evaluate the empty cell.

    In regard to the other problem, if the insertion point is between a hard return and a Section Break (Next Page), how can I test for that programmatically? I see I can use MoveLeft and MoveRight to get there, but then I don't know what to do after that.

    Also, is there a list in Help of the important Chr codes like the ones you told me about? I couldn't find a list.

    Thanks so much!

    - Linda

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: How do you evaluate what's at the insertion po

    Selection.Characters.First.Previous will give you the character to the left of the Selection. (This works whether the Selection is a block of text or an Insertion Point.)

    A few of the more common special Chr() values in Word are

    11 -- manual line break (aka soft return)
    12 -- manual page break (or section break)
    13 -- paragraph mark
    13 & 7 -- end-of-cell marker
    14 -- column break
    30 -- non-breaking hyphen
    31 -- optional hyphen
    160 -- non-breaking space

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Thanks!

    Thanks, everyone, for all the great advice!

    - Linda

Posting Permissions

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