Pregunta

If I have some VB Component open in the Excel VBE, with some text highlighted as shown,

enter image description here

Is there a way to programmatically grab the text "zBool"?

Preferrably NOT using Sendkeys

6x New Zealand Steinlager Beers (a good solution), for another good solution

Q: Why do black sheep eat less than white sheep?

A: Because there aren't as many of them

¿Fue útil?

Solución

Add a reference to "Microsoft visual basic for applications extensibility..."

Sub Tester()

Dim oVBE As vbe
Dim startLine As Long, startCol As Long
Dim endLine As Long, endCol As Long
Dim sContent As String, tmp As String, l As Long
    Set oVBE = Application.vbe

    oVBE.ActiveCodePane.GetSelection _
        startLine, startCol, endLine, endCol

    For l = startLine To endLine
        tmp = oVBE.ActiveCodePane.CodeModule.Lines(l, 1)
        If l = endLine Then tmp = Left(tmp, endCol - 1)
        If l = startLine Then tmp = Right(tmp, (Len(tmp) - startCol) + 1)
        sContent = sContent & IIf(Len(sContent) > 0, Chr(10), "") & _
                  tmp
    Next l


    Debug.Print sContent

End Sub

GetSelection method: http://msdn.microsoft.com/en-us/library/aa443954(v=vs.60).aspx

See here for how to use that returned information to access the actual text: http://www.cpearson.com/excel/vbe.aspx (I'm sure it's there somewhere...)

EDIT - did that for you for my own education...
I'll put it on your tab ;-)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top