你能否以编程方式访问Excel VBE选择?(Can You Access the Excel VBE Selection Programatically?)

编程入门 行业动态 更新时间:2024-10-28 18:24:11
你能否以编程方式访问Excel VBE选择?(Can You Access the Excel VBE Selection Programatically?)

如果我在Excel VBE中打开了一些VB组件,并且如图所示突出显示了一些文本,

有没有办法通过编程获取文本“zBool”?

最好不要使用Sendkeys

6x新西兰Steinlager啤酒(一个很好的解决方案),为另一个好的解决方案

问:为什么黑羊比白羊少吃?

答:因为没有那么多

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

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

最满意答案

添加对“Microsoft Visual Basic for Applications扩展性...”的引用

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方法: http : //msdn.microsoft.com/en-us/library/aa443954( v=vs.60) .aspx

请参阅此处了解如何使用返回的信息访问实际文本: http : //www.cpearson.com/excel/vbe.aspx (我确定它在某处...)

编辑 - 为了我自己的教育,为你做了这个... 我会把它放在你的标签上;-)

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 ;-)

更多推荐

本文发布于:2023-08-03 03:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   Excel   VBE   Programatically   Selection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!