如何从宏中读取当前行(光标所在的位置)的文本?
我将使用这样的功能:
Public Sub AddTextToChangeLogFile()
Dim textOnACurrentLine As ???
textOnACurrentLine = ???
If textOnACurrentLine.Text <> String.Empty Then
Dim sw As New StreamWriter("C:\###\Changes.txt", True)
sw.WriteLine(textOnACurrentLine + ". file: " + DTE.ActiveDocument.Name)
sw.Close()
End If
End Sub
答案 0 :(得分:4)
您可以使用以下内容:
Dim textOnACurrentLine As String
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.EndOfLine(True)
textOnACurrentLine = DTE.ActiveDocument.Selection.Text
答案 1 :(得分:3)
从Selection中获取行索引并使用EditPoint,如下所示:
TextSelection text_selection = (TextSelection)m_DTE.ActiveDocument.Selection;
int line_index = text_selection.ActivePoint.Line;
TextDocument text_doc = (TextDocument)m_DTE.ActiveDocument.Object("");
EditPoint edit_point = text_doc.CreateEditPoint();
string line = edit_point.GetLines(line_index, line_index+1);