2010-03-29 76 views
2

如何從宏讀取當前行(光標所在的位置)的文本?Visual Studio宏的當前行

我會使用這樣的功能:

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 

回答

4

您可以使用類似:

Dim textOnACurrentLine As String 
DTE.ActiveDocument.Selection.StartOfLine(0) 
DTE.ActiveDocument.Selection.EndOfLine(True) 
textOnACurrentLine = DTE.ActiveDocument.Selection.Text 
+0

好主意!工作正常 – Vadim 2010-05-14 15:39:10

+1

這有副作用,離開選定的行。一個更好的選擇是使用na EditPoint – 2012-11-22 23:42:23

3

獲取從選擇行索引和使用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); 
+0

感謝這個改進的答案,作品一種享受。 – 2014-05-06 21:33:24