Macro to modify currently selected text does not work correctly in Word (292152)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002

This article was previously published under Q292152

SYMPTOMS

When you use a Visual Basic for Application macro that performs an operation on the currently selected text, the macro may not function as expected.

CAUSE

This behavior can occur when both of the following conditions are true:
  • The selection is discontiguous. There are two or more selections of text.
  • The Selection property or method being used does not apply across discontiguous selections.
Some properties and methods of the Selection object only apply to a single selection or the most recently selected text.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

For more information how to use the sample code in this article, click the following article number to view the article in the Microsoft Knowledge Base:

290140 How to run the sample code for the Office XP programs from Knowledge Base articles

You can reduce a discontiguous selection to a selection of the most recently selected text by using the ShrinkDiscontiguousSelection method.

The following macro example changes the selection to the most recently selected text and displays the character count:
Sub A()

   Selection.ShrinkDiscontiguousSelection
   MsgBox Selection.Characters.Count

End Sub
				

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

For more information about how to select discontiguous text, click the following article number to view the article in the Microsoft Knowledge Base:

286789 How to select items that are not next to each other


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug kbpending KB292152