Originally Posted by
Emily
xld,
Suppose I selected "With Application", now the code displays the whole line. I just want "With Application".
Sorry, that was sloppy omission on my part. I have also added some code if just 2 lines are selected
Sub test()
Dim txt
' Ref: Microsoft Visual Basic for Applications Extensibility 5.3
Dim StartLine As Long
Dim EndLine As Long
Dim StartCol As Long
Dim EndCol As Long
Dim i As Long
With Application.VBE.ActiveCodePane.CodeModule
.CodePane.GetSelection StartLine, StartCol, EndLine, EndCol
txt = Right(.Lines(StartLine, 1), Len(.Lines(StartLine, 1)) - StartCol)
If StartLine = EndLine Then
txt = Mid(.Lines(StartLine, 1), StartCol, EndCol - StartCol)
Else
If EndLine - StartLine > 1 Then
For i = StartLine + 1 To EndLine - 1
txt = txt & vbCrLf & .Lines(i, 1)
Next i
End If
txt = txt & vbCrLf & Left(.Lines(EndLine, 1), EndCol)
End If
MsgBox "Selected text in VBE Window: " & vbCrLf & vbCrLf & txt
End With
End Sub