#1
|
|||
|
|||
Possible bug in Word VBA "selection.expand" method
While developing Word macros that act on a sentence, I may have found a bug in the "selection.expand" method. The bug impacts the reliability of my scripts, and probably breaks a built-in Word command. (I may post on that later.)
Steps to reproduce are below. I’m using Word 2019. I’m curious whether this "issue" affects older and newer versions of Word. If you try to reproduce this bug, please include the version of Word you're using. The bug is easier to spot if the paragraph codes are toggled on. The default hotkey is Ctrl + Shift + 8. Steps to reproduce: 1. Open a new Word document. 2. Type in several sentences and several paragraphs. Press Enter twice after one of the paragraphs. See attachment for an example. 3. Create two VBA scripts: Sub TestSelectParagraph() Selection.Expand Unit:=wdParagraph End Sub Sub TestSelectSentence() Selection.Expand Unit:=wdSentence End Sub 4. Place the cursor anywhere within the sentence that contains the word "sentence." (In my example, this sentence is highlighted in yellow.) 5. Run "TextSelectParagraph." It works as expected: Only the paragraph gets selected. 6. Unselect. 7. As before, place the cursor anywhere within the sentence that contains the word "sentence." 8. Run "TextSelectSentence." The sentence PLUS two carriage returns get selected. In other words, the selection consists of one sentence AND two blank paragraphs. This is an inconsistency, as a sentence is a smaller unit of text than a paragraph. But VBA is treating a sentence as having more text than a paragraph. If you press Enter 1000 times after entering a paragraph, "Selection.Expand Unit:=wdSentence" will select the sentence AND the 1000 blank paragraphs! This behaviour isn't logical. Blank lines that follow a sentence don't belong to the sentence. Each blank line is a paragraph in its own right. In English at least, a sentence always ends with one of three terminal punctuation marks: a period, a question mark, or an exclamation mark. Here's another example: 9. Move the cursor to the paragraph mark between the two paragraphs. 10. Run both macros. Again, the macro that selects a sentence selects more text than the macro that selects an entire paragraph. The paragraph selection is correct, and the sentence selection is wrong. Expected behaviour: When a sentence ends with a carriage return, "Selection.Expand Unit:=wdSentence" should select up to, and include, the sentence's terminal punctuation. But not blank paragraphs that follow the sentence. If a sentence at the end of a paragraph is missing or doesn't have a terminal punctuation mark, "Selection.Expand Unit:=wdSentence" should select up to, but not include, the carriage return. (Or maybe the selection should include one carriage return?) There is another bug, probably related, with "selection.extend." But that’s for another day! |
#2
|
|||
|
|||
This bug has now been confirmed in Word 2003, which was released about 22 years ago. So it's likely a "legacy bug" that has gone unnoticed since the early days of Word VBA.
What is the likelihood that Microsoft will want to fix the bug if I were to report it? Close to zero. The bug probably doesn't impact too many users. (Although it does affect me!) Developers who have created macros that rely on selection.expand may have already incorporated workarounds. I just tested the "select sentence" command in Dragon Professional, and it is not subject to the bug. My guess is that Nuance developers knew about the problem with selection.expand and built the "select sentence" command to compensate for this limitation. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Title case macro gets a "type mismatch" error at the Instr method | marceepoo | Word VBA | 1 | 03-05-2020 09:41 PM |
typing "scr" and problem with "extending selection" | danielmatt | Word | 16 | 07-21-2019 12:31 PM |
help with compatibility of "WdSaveFormat" property of SaveAs method | ajetrumpet | Word | 0 | 07-15-2013 07:29 AM |
"Replace all" within a selection (Word 2007) | paulkaye | Word | 5 | 02-19-2013 01:25 AM |
Expand & Collapse Folders in "Unread Mail" | WoodstockCEO | Outlook | 3 | 02-14-2010 09:11 PM |