PDA

View Full Version : [SOLVED] Is it possible to display the highlight text in VBE



Emily
05-04-2005, 08:54 AM
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

Ken Puls
05-04-2005, 09:11 AM
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.

Bob Phillips
05-04-2005, 09:42 AM
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.

Emily
05-04-2005, 10:04 AM
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

Bob Phillips
05-04-2005, 10:15 AM
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

Ken Puls
05-04-2005, 10:22 AM
Sorry Emily! I didn't understand exactly what you were after.

Nice work, xld!

Emily
05-04-2005, 10:46 AM
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

Bob Phillips
05-04-2005, 10:57 AM
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

Emily
05-04-2005, 11:04 AM
xld:

I really appreciate your help

Cyberdude
05-04-2005, 11:28 AM
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. :bug:

Ken Puls
05-04-2005, 11:45 AM
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. :bug:

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 (http://www.cpearson.com/excel/vbe.htm)

HTH,

Emily, you can have your thread back now! ;)

Emily
05-04-2005, 11:56 AM
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.

Bob Phillips
05-04-2005, 12:03 PM
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.

Bob Phillips
05-04-2005, 02:43 PM
Emily,

You read Chinese?

Edited by Firefytr: Thread Split

Emily
05-04-2005, 07:56 PM
Emily,

You read Chinese?

Edited by Firefytr: Thread Split

I am a Chinese from Hong Kong.

Edited by Firefytr: Thread Split

Emily
05-05-2005, 01:28 AM
The flag was added after your post.

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

Zack Barresse
05-05-2005, 11:05 AM
Originally posted by Kpuls, as edited by Firefytr in Thread Split/Merge:


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:

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.