views:

351

answers:

1

I'm writing a macro to let me replace the spaces in a string in my code file with underscores. I've gotten as far as finding the beginning and end of the string as instances of VirtualPoint. Now I'm trying to select from the first VirtualPoint to the second. And I can't figure it out.

I know the VirtualPoints are correct, because I'm using MessageBox.Show to tell me their values when I run the macro. I just don't know the correct command to set the TextSelection from the first to the second. I've tried this:

selection.MoveToPoint(firstVirtualPoint)
selection.MoveToPoint(secondVirtualPoint, True)

This seems like it ought to work, but it doesn't. The cursor just moves to the end of the line (as far as I can tell).

Does anybody know the correct command for doing this?

+1  A: 

As these things tend to go, after I give up, suddenly it hits me. Perhaps this will help someone else, though.

A fuller sample of the code is this:

Dim selection As TextSelection = 
    CType(DTE.ActiveDocument.Selection, TextSelection)
selection.StartOfLine()

selection.FindText("some string at start")
Dim pointAfterStart = selection.BottomPoint

selection.FindText("some string at end")
Dim pointBeforeEnd = selection.TopPoint

selection.MoveToPoint(pointAfterIt)
selection.MoveToPoint(pointBeforeLambda, True)

The idea is to find the initial text, then the ending text, and then select everything in between. What I found in the debugger was that the values in pointAfterStart and pointBeforeEnd were changing. Perhaps deceived by the name (since System.Drawing.Point is a struct), I wasn't realizing they were references that pointed to the current selection position.

I solved it this way:

selection.FindText("It ")
Dim pointAfterIt = selection.BottomPoint.CreateEditPoint

selection.FindText(" = () =>")
Dim pointBeforeLambda = selection.TopPoint.CreateEditPoint

This made copies of the selection points, so that they didn't change when the selection moved later.

Kyralessa