Consulting

Results 1 to 17 of 17

Thread: Is it possible to display the highlight text in VBE

  1. #1
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location

    Is it possible to display the highlight text in VBE

    The code below simulate a selection of text (it is "Selection") in VBE and display.

    My question is, is it possible to display the highlight text in VBE?

    Thanks in advance


    Sub test()
        ' 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
        With Application.VBE.ActiveCodePane.CodeModule
            .CodePane.SetSelection 8, 22, 8, 31  ' Set selected string
            .CodePane.GetSelection StartLine, StartCol, EndLine, EndCol
            txt = Mid(.Lines(StartLine, 1), StartCol, EndCol - StartCol)
            MsgBox "Selected text in VBE Window: " & txt
        End With
    End Sub

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    I guess you could change this:

    MsgBox "Selected text in VBE Window: " & txt
    To:

    debug.print "Selected text in VBE Window: " & txt
    That would print it in the Immediate window.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Emily
    The code below simulate a selection of text (it is "Selection") in VBE and display.

    My question is, is it possible to display the highlight text in VBE?
    Emily,

    I am not quoite sure what you are attempting. Do you want to be able to have a button (say) that outputs (however that is done) the curent text selected in VBE? If so, can I ask why, as it seems the fact that it is selected is sufficient (at least for me it would be )

    Guessing a bit, but is this what you are after?

    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)
    For i = StartLine + 1 To EndLine - 1
    txt = txt & vbCrLf & .Lines(i, 1)
    Next i
    txt = txt & vbCrLf & Left(.Lines(EndLine, 1), EndCol)
    MsgBox "Selected text in VBE Window: " & vbCrLf & vbCrLf & txt
    End With
    End Sub


    PS You don't need the reference to Microsoft Visual Basic for Applications Extensibility 5.3, you would only need that if you declare any object variables of a specific type from that library, and that is not necessary, as it will late-bind with Dim xx As Object quite nicely.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Thanks, kpuls. Not debug.print

    Suppose I have another code

    Sub Emily()
          ' This is a highlighted text
          MsgBox Application.VBE.ActiveCodePane.CodeModule.Lines(2, 1)
    End Sub

    I want a code to display the highlighted text (red) in message box or the position and length of the text if possible.

    xld,
    Thanks a lot, your code (a little bit changed) is the one I am looking for.



    Sub test()
        Dim txt
        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 + 1)
            For i = StartLine + 1 To EndLine - 1
                txt = txt & vbCrLf & .Lines(i, 1)
            Next i
            MsgBox "Selected text in VBE Window: " & vbCrLf & vbCrLf & txt
        End With
    End Sub

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Oh heck. what's one character between friends.

    I see you have dropped the Endline, did you mean to do that? If you wanted all of the last line, not just to Endcol, then use

    For i = StartLine + 1 To EndLine
    instead of

    For i = StartLine + 1 To EndLine - 1
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Sorry Emily! I didn't understand exactly what you were after.

    Nice work, xld!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    xld,

    Sorry, would you please improve the code a little bit.

    Suppose I selected "With Application", now the code displays the whole line. I just want "With Application".


    txt = Right(.Lines(StartLine, 1), Len(.Lines(StartLine, 1)) - StartCol + 1)
    If 1 is not added to StartCol, the first character W will be missed.

    Thanks.
    Emily

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote 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
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    xld:

    I really appreciate your help

  10. #10
    Sorry, but I just don't understand what you guys are doing, but it looks like something I want to know more about.
    Is Emily trying to write a macro that will modify the code in another macro? I'd sure like to know more about how to do that.

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by Cyberdude
    Sorry, but I just don't understand what you guys are doing, but it looks like something I want to know more about.
    Is Emily trying to write a macro that will modify the code in another macro? I'd sure like to know more about how to do that.
    Hi Sid,

    Emily actually just wants to display text that she's highlighted in the VBE in a messagebox. A little different.

    You want code to write/modify/delete code? Check out Chip Pearson's examples

    HTH,

    Emily, you can have your thread back now!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Not to modify the code, one Chinese Excel expert (TaiWan http://www.vba.com.tw/plog/) want to find the shortest way to get another trigger from highlighted text in module.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I know what she is doing, but I don't know why.

    When in the VBE, you can highlight part of the code (as I am sure that you know).

    Emily has a macro that can show that text, from start character to end character, in a MsgBox. This could be tied to a VBE toolbar button for easy firing.

    I don't know wht because if it is highlighted, I would have thought it is simpler to just look at the highlighted text :-). Of course, it might be the foundation for something much bigger, some sort of tracking/debugging application.

    If you want to know how to modify code on the fly, post an example of what you want to do here, or in an Excel newsgroup, help in doing that would probably be the best way for you to learn.

    It's a pleasure Emily.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Emily,

    You read Chinese?

    Edited by Firefytr: Thread Split
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Quote Originally Posted by xld
    Emily,

    You read Chinese?

    Edited by Firefytr: Thread Split
    I am a Chinese from Hong Kong.

    Edited by Firefytr: Thread Split

  16. #16
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    The flag was added after your post.

    I think it should be checked by the Administrator. - done! (-Firefytr)

  17. #17
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Originally posted by Kpuls, as edited by Firefytr in Thread Split/Merge:

    Quote Originally Posted by kpuls
    Oh! And btw... did you guys know that the Chineese and Japaneese versions of MahJong are totally different? (And I'm talking the real game, not that computerized "match pairs" version we see on the net)

    Our family plays one version (I think the Chineese version) where you try to collect Pungs (3 of a kind) Qwongs (4 of a kind) or Chows (runs of 3). The other version is all runs of 4's, I think. As I say, our family has played for years. We had a Japaneese exchange student one summer, asked him if he knew how to play, and we both ended up playing our versions until we figured out they were different! LOL!

    As originally posted by Dreamboat:
    Quote Originally Posted by Dreamboat
    Emily... Hi! I still haven't figured out the Mahjong yet. And sorry about the off-topic posts! Probably kpuls or firefytr will move them out of here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •