ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5780|回复: 7

[原创]VBE中文代码复制器

[复制链接]

TA的精华主题

TA的得分主题

发表于 2004-11-23 05:48 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖已被收录到知识树中,索引项:VBE环境开发

[原创]VBE中文代码复制器

[原创]VBE中文代码复制器

[原创]VBE中文代码复制器

[原创]VBE中文代码复制器

bmsIMBqm.rar

9.63 KB, 下载次数: 161

[原创]VBE中文代码复制器

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-11-23 05:49 | 显示全部楼层

以下代码供参考:

'**** I Love You_Word! Create 2004-11-23  ****

'^[ThisDocument-ThisDocument]^'

Dim MyVbeProject As MyClass

Sub AutoExec()

On Error Resume Next

'加载时自动加载"VBIDE"库文件和运行AddMybar的过程

Me.VBProject.References.AddFromFile "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

'引用VBIDE库文件,引用名为"Microsoft Visual Basic for Applications Extensibility 5.3",版本不同可能略有差异

AddMyBar

End Sub

'----------------------

Sub AutoExit()

On Error Resume Next

'卸载时自动去除"VBIDE"引用

With Me.VBProject

Set ref = .References("VBIDE")

If Not ref Is Nothing Then

.References.Remove ref

End If

End With

DelMyBar

End Sub

'----------------------

Sub AddMyBar()

Dim MyBar As CommandBarControl

On Error Resume Next

DelMyBar '先删除后增加

Set MyBar = Application.VBE.CommandBars("Code Window").Controls.Add

With MyBar

.Caption = "GetCopy"

.FaceId = 19

Set MyVbeProject = New MyClass '将MyVbeProject定义为新MyClass类

Set MyVbeProject.MyProject = Application.VBE.Events.CommandBarEvents(MyBar)

'此句代码意为该MyvbeProject类中的MyProject过程是指向(响应)命令MyBar的单击事件

'MyBar是该事件的源对象,此事件相当于CommandControl的OnAction属性 End With

End With

End Sub

'----------------------

Sub DelMyBar()

On Error Resume Next

'删除VBE右键中的一个"GetCopy"的命令按钮,使其还原

Application.VBE.CommandBars("Code Window").Controls("GetCopy").Delete

End Sub

'----------------------

'**** I Love You_Word! Create 2004-11-23  ****

'^[类模块-MyClass]^'

Public WithEvents MyProject As VBIDE.CommandBarEvents

Private Sub MyProject_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)

Dim i As Integer, Avs As Object, strSub As String, ComType As String

Set Avs = Application.VBE.SelectedVBComponent

i = Avs.CodeModule.CountOfLines

Select Case Avs.Type

Case 1

ComType = "标准模块"

Case 2

ComType = "类模块"

Case 3

ComType = "用户窗体"

Case 100

ComType = "ThisDocument"

Case Else

ComType = "未知模块"

End Select

With Selection

.Collapse Direction:=wdCollapseEnd

.InsertAfter "'**** " & Application.UserName & " Create " & Date & " " & _

" ****" & vbCrLf

.InsertAfter "'^[" & ComType & "-" & Avs.Name & "]^'" & vbCrLf

.Font.Bold = True

For n = 1 To i

strSub = Avs.CodeModule.Lines(n, 1)

If strSub Like "End Sub*" = True Or strSub Like "End Type*" = True Or _

strSub Like "End Function*" = True Then strSub = strSub & vbCrLf & "'----------------------"

.InsertAfter strSub & vbCrLf

Next

.Font.Name = "Tahoma"

.Font.Size = 12

.Font.Color = wdColorBlue

.Paragraphs(1).Range.Font.Color = wdColorRed

.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

.Cut

End With

End Sub

'----------------------

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-11-25 05:16 | 显示全部楼层

经Taller版主指点,重新改写了部分代码,自动判断是否有选中内容,如果有选中内容,则自动转换复制选中部分(整行),如果未予选中则进行所有当前模块中的所有代码进行转换复制。

WS6d5On5.rar (13.87 KB, 下载次数: 72)

以下代码供参考:

'**** I Love You_Word! Create 2004-11-25  ****

'^[ThisDocument-ThisDocument]^'

Dim MyVbeProject As MyClass

Sub AutoExec()

On Error Resume Next

'加载时自动加载"VBIDE"库文件和运行AddMybar的过程

Me.VBProject.References.AddFromFile "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

'引用VBIDE库文件,引用名为"Microsoft Visual Basic for Applications Extensibility 5.3",版本不同可能略有差异

AddMyBar

End Sub

'----------------------

Sub AutoExit()

On Error Resume Next

'卸载时自动去除"VBIDE"引用

With Me.VBProject

Set ref = .References("VBIDE")

If Not ref Is Nothing Then

.References.Remove ref

End If

End With

DelMyBar

End Sub

'----------------------

Sub AddMyBar()

Dim MyBar As CommandBarControl

On Error Resume Next

DelMyBar '先删除后增加

Set MyBar = Application.VBE.CommandBars("Code Window").Controls.Add

With MyBar

.Caption = "GetCopy"

.FaceId = 19

Set MyVbeProject = New MyClass '将MyVbeProject定义为新MyClass类

Set MyVbeProject.MyProject = Application.VBE.Events.CommandBarEvents(MyBar)

'此句代码意为该MyvbeProject类中的MyProject过程是指向(响应)命令MyBar的单击事件

'MyBar是该事件的源对象,此事件相当于CommandControl的OnAction属性 End With

End With

End Sub

'----------------------

Sub DelMyBar()

On Error Resume Next

'删除VBE右键中的一个"GetCopy"的命令按钮,使其还原

Application.VBE.CommandBars("Code Window").Controls("GetCopy").Delete

End Sub

'----------------------

'**** I Love You_Word! Create 2004-11-25  ****

'^[类模块-MyClass]^'

Public WithEvents MyProject As VBIDE.CommandBarEvents

Private Sub MyProject_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)

Dim i As Long, Avs As Object, strSub As String, ComType As String

Dim RowStart As Long, ColStart As Long, RowEnd As Long, ColEnd As Long

With Application

.ScreenUpdating = False

Set Avs = .VBE.SelectedVBComponent

.VBE.ActiveCodePane.GetSelection RowStart, ColStart, RowEnd, ColEnd

If RowStart = RowEnd And ColStart = ColEnd Then

m = 1: n = Avs.CodeModule.CountOfLines

ElseIf ColEnd = 1 Then

m = RowStart: n = RowEnd - 1

Else

m = RowStart: n = RowEnd

End If

Select Case Avs.Type

Case 1

ComType = "标准模块"

Case 2

ComType = "类模块"

Case 3

ComType = "用户窗体"

Case 100

ComType = "ThisDocument"

Case Else

ComType = "未知模块"

End Select

With Selection

.Collapse Direction:=wdCollapseEnd

.InsertAfter "'**** " & Application.UserName & " Create " & Date & " " & _

" ****" & vbCrLf

.InsertAfter "'^[" & ComType & "-" & Avs.Name & "]^'" & vbCrLf

.Font.Bold = True

For i = m To n

strSub = Avs.CodeModule.Lines(i, 1)

If strSub Like "End Sub*" = True Or strSub Like "End Type*" = True Or _

strSub Like "End Function*" = True Then strSub = strSub & vbCrLf & "'----------------------"

.InsertAfter strSub & vbCrLf

Next

.Font.Name = "Tahoma"

.Font.Size = 11

.Font.Color = wdColorBlue

.Paragraphs(1).Range.Font.Color = wdColorRed

.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

.Cut

End With

.ScreenUpdating = True

End With

End Sub

'----------------------

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-11-28 05:59 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

mqIuLXJO.rar (17.15 KB, 下载次数: 128)

根据办公之星的提醒,已将复制代码中的回车符改为手动换行符,并添加了一个代码头和小说明。 效果如下:

'* +++++++++++++++++++++++++++++++++++++++

'* Created By 守柔@ExcelHome 2004-11-28 6:09:58

'仅测试于System: Windows NT Word: 10.0 Language: 2052

'^The Code CopyIn [ThisDocument-ThisDocument]^'

'* --------------------------------------------------------------------------

Sub Test() Dim i As Integer, Avs As Object, strSub As String Set Avs = Application.VBE.SelectedVBComponent i = Avs.CodeModule.CountOfLines With Selection .Collapse Direction:=wdCollapseEnd For n = 1 To i strSub = Avs.CodeModule.Lines(n, 1) .InsertAfter strSub & Chr(13) Next .Copy .Delete End With End Sub '----------------------

[此贴子已经被作者于2004-11-28 6:11:02编辑过]

TA的精华主题

TA的得分主题

发表于 2004-12-28 20:01 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
看后糊涂,太难明白.

TA的精华主题

TA的得分主题

发表于 2004-12-28 21:24 | 显示全部楼层

那就一起慢慢消化吧,谢谢守柔的作品,收藏!!!

TA的精华主题

TA的得分主题

发表于 2004-12-29 22:14 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
好象word2003对中文支持的很好

TA的精华主题

TA的得分主题

发表于 2009-5-23 11:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
老大的工程加了密
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-23 21:09 , Processed in 0.039743 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表