代码窗口中包含的代码窗格。代码窗口被用来输入和编辑代码。代码窗口可含有多个代码窗格。
用 CodePane 对象来操作 CodePane 中代码或选取的代码或文本。
5.1 显示代码窗格
5.1.1 显示当前代码窗格
'打开并显示当前代码窗格
Public Sub ShowProject ()
VBE.ActiveCodePane.Show
End Sub
5.1.2 显示指定部件代码模块窗格
'函数功能:打开指定部件代码模块窗格
Public Sub ShowComponent (CompsNameOrIndex As Variant)
Dim VBProj As VBProject '工程项目对象
Dim VBComp As VBComponent '组件对象
Dim CodeMod As CodeModule '代码模块
Dim VBCodePane As CodePane '窗格对象
'实例化对象
Set VBProj = VBE.ActiveVBProject
Set VBComp = VBProj.VBComponents (CompsNameOrIndex)
Set CodeMod = VBComp.CodeModule
Set VBCodePane = CodeMod.CodePane
VBCodePane.Show '显示代码窗格
End Sub
5.2 获取窗格所选代码行列信息
5.2.1 获取当前窗格中所选代码起止行列信息
'所选代码的起止行列信息定义数据类型
Public Type SelLineColInfo
SLine As Long '起始行
SCol As Long '起始列
ELine As Long '结束行
ECol As Long '结束列
End Type
'-----------------------------------------------------------------------
'函数功能:获得所选代码开始行列及结束行列信息
Public Function VBGetSelection () As SelLineColInfo
Dim SelInfo As SelLineColInfo '数据类型
VBE.ActiveCodePane.GetSelection SelInfo.SLine, SelInfo.SCol, _
SelInfo.ELine, SelInfo.ECol
'获取的行列信息输出
VBGetSelection = SelInfo
End Function
'***************************************************
'调用示例:在窗格中任选一处代码行列,再运行以下代码
Dim SelInfo As SelLineColInfo '申明自定数据类型
'起止行列信息赋值给变量
SelInfo = VBGetSelection
'输出显示
MsgBox "起始行:" & SelInfo.SLine & vbLf & _
"起始列:" & SelInfo.SCol & vbLf & _
"结束行:" & SelInfo.ELine & vbLf & _
"结束列:" & SelInfo.ECol
上一页 下一页