Share via

Microsoft Visual Basic for Applications in MS Word 2010

Anonymous
2016-05-05T11:21:53+00:00

Hello,

I create a new Word 2010 document containing only the 5 character string "abcde". I place the cursor between the "b" and "c" characters. No characters are selected. I then open the "Microsoft Visual Basic for Applications" window by typing Alt-F11 and go to the "Immediate" window.

If I type "? selection.text" the "c" character is displayed.   I would expect an empty string to be returned.

If I type "? LEN(selection.text)" the number 1 is displayed.   I would expect the number 0 to be displayed.

If I type "? selection.start" the number 2 is displayed.   This is what I would expect.    

If I type "? selection.end" the number 2 is also displayed.   This is what I would expect.

My question is why does Word 2010 return the character "c" when I type "? selection.text" even though no characters are selected.

Thank you for any help you can provide.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2016-05-06T14:12:47+00:00

    If that's how Word 2010 is designed then I would argue Microsoft got  it wrong.

    You can argue that if you wish, but it's been that way for as long as Word has had VBA. If you think MS should change it, you can post a suggestion at: http://word.uservoice.com/

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-05-06T13:35:55+00:00

    Thanks for responding Paul.

    If that's how Word 2010 is designed then I would argue Microsoft got  it wrong. Here is my argument.

    It my example of the cursor positioned between "b" and "c":

      selection.Start = 2

      selection.End = 2

      selection.Type = 1   (wdSelectionIP)

    Therefore, Word has confirmed there is no character selected but responds with:

      selection.Text = "c"

    Now take the case where the character "c" is selected:

      selection.Start = 2

      selection.End = 3

      selection.Type = 2  (wdSelectionNormal)

    In this case Word has confirmed there is a character selected and responds with:

      selection.Text = "c"

    I argue Word can't have it both ways. It's illogical to return "c" if Start=2 and End=2 and at the same time return "c" if Start=2 and End=3. If Start=2 and End=2 then "selection.Text" should return an empty string.

    Here is an example of Word correctly returning an empty string when the "Range" method is added to the above operations. Using my initial example of the cursor positioned between "b" and "c":

      selection.Range.Start = 2

      selection.Range.End = 2

      selection.Range.Text = ""

    Why would Microsoft design "selection.Text" and "selection.Range.Text" to return different results when no text is selected? Note that this discrepancy only occurs if no text is selected in the document. If text is selected then "selection.Text" and "selection.Range.Text" return the same result.

    I have to conclude that "selection.Text" returning a character when no characters are selected is a design flaw.

    Thank you for your patience.

    Was this answer helpful?

    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2016-05-06T03:47:28+00:00

    My question is why does Word 2010 return the character "c" when I type "? selection.text" even though no characters are selected.

    Because that's how it's designed. If you want to test what the selection encompasses, use code like:

    Dim StrRpt As String

    With Selection

      Select Case .Type

        Case wdSelectionIP: StrRpt = "Insertion Point Selection"

        Case wdNoSelection: StrRpt = "No Selection"

        Case wdSelectionNormal: StrRpt = "Normal Selection: " & .Text

        Case wdSelectionBlock: StrRpt = "Block Selection"

        Case wdSelectionColumn: StrRpt = "Column Selection"

        Case wdSelectionRow: StrRpt = "Row Selection"

        Case wdSelectionInlineShape: StrRpt = "InlineShape Selection"

        Case wdSelectionShape: StrRpt = "Shape Selection"

        Case wdSelectionFrame: StrRpt = "Frame Selection"

      End Select

    End With

    MsgBox StrRpt

    Was this answer helpful?

    0 comments No comments