汉字转拼音的完美解决方案可以更完美,Word版飞快注音
时间:2009-03-20
来源:互联网
使用查找到方法进行,可以过滤无关到字符。
欢迎大家修改代码,看看到底还能有多快!
注意:需要微软拼音输入法2003到支持,微软拼音输入法2007下可能造成程序失去响应!为获得最佳速度,请切换到普通视图。
对汉字转拼音的完美解决方案中的到认知还是极为肤浅的,如果能够再深入了解,让修改后到代码既可以识别多音字,又能够插入空格就更好了。
[ 本帖最后由 wjhere 于 2009-3-21 06:17 编辑 ]
附件

2009-3-20 22:10, 下载次数: 357
作者: wjhere 发布时间: 2009-03-20
'Dim wL, K As Integer
S = Timer
Application.ScreenUpdating = False '这句提前了,能节约一点时间
ActiveDocument.ActiveWindow.View.Type = wdNormalView
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "[一-﨩]{1,}" '这句是什么意思?楼主能解释一下吗?
.MatchWildcards = True
Do While .Execute
Set myRr = .Parent
' Word.Application.StatusBar = myRr.Text '屏蔽了状态栏回显,能节约较多时间
没有做这些小改动前,耗时7秒,之后 耗时 4秒

[ 本帖最后由 coby001 于 2009-3-20 23:40 编辑 ]
作者: coby001 发布时间: 2009-03-20
作者: sylun 发布时间: 2009-03-21
引用:
原帖由 coby001 于 2009-3-20 23:35 发表Dim S# ' 双精度比较准确
'Dim wL, K As Integer
S = Timer
Application.ScreenUpdating = False '这句提前了,能节约一点时间
ActiveDocument.ActiveWindow.View.Type = wdNormalView
ActiveDocument.ActiveW ...
您的代码的确很快。
作者: wjhere 发布时间: 2009-03-21
作者: wjhere 发布时间: 2009-03-21

作者: 冰风萧萧 发布时间: 2009-03-21

作者: 冰风萧萧 发布时间: 2009-03-21
引用:
原帖由 冰风萧萧 于 2009-3-21 10:34 发表没用微软拼音,9秒,都够快了

作者: coby001 发布时间: 2009-03-21
'根据sylun兄提供KSPPtoASCII3修改
'sylun兄已经发给我最快的测试文档仅3秒多。(因电脑而异)没有得到授权,不宜公开代码
'得到了论坛朋友们的帮助。这个是原来的改进版,改用倒序查找的方法速度达到5秒。
'2009.3.21 [email protected]
Dim strW As String
Dim strP As String
Dim myR, myRr As Range
Dim S#
Dim wL, K As Integer
S = Timer
On Error Resume Next
Application.ScreenUpdating = False '关闭屏幕刷新
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True '关闭域代码显示
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "[一-﨩]{1,}" '查找一个以上的中文字符
.MatchWildcards = True
.Forward = False '使用通配符,倒序查找
Do While .Execute
Set myRr = .Parent '找到的中文字符串区域
wL = myRr.Words.Count
For K = wL To 1 Step -1 '使用倒序标注拼音
Set myR = myRr.Words(K)
strW = myR.Text
strP = HzToPy(strW)
myR.PhoneticGuide Text:=strP, Alignment:=wdPhoneticGuideAlignmentCenter, _
Raise:=15, FontSize:=14
myR.Collapse wdCollapseStart
Next
myRr.Collapse wdCollapseStart ' 折叠到开头,避免二次查找
Loop
End With
Set myR = Nothing
Set myRr = Nothing
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
MsgBox "用时 " & Timer - S & " 秒!", vbInformation, "飞快标注音标"
附件

2009-3-21 13:16, 下载次数: 90
作者: wjhere 发布时间: 2009-03-21
附件

2009-3-21 13:23
作者: coby001 发布时间: 2009-03-21
Set myR = myRr.Words(K)
strW = myR.Text
strP = HzToPy(strW) '有个想法,能不能把这个函数直接在当前过程里实现,而不是调用,减少无谓的消耗
myR.PhoneticGuide Text:=strP, Alignment:=wdPhoneticGuideAlignmentCenter, _
Raise:=15, FontSize:=14
myR.Collapse wdCollapseStart
Next
作者: coby001 发布时间: 2009-03-21
附件

2009-3-21 13:54, 下载次数: 58
作者: wjhere 发布时间: 2009-03-21
附件

2009-3-21 14:07
作者: coby001 发布时间: 2009-03-21
作者: coby001 发布时间: 2009-03-21
作者: wjhere 发布时间: 2009-03-21
作者: wjhere 发布时间: 2009-03-21
作者: coby001 发布时间: 2009-03-21
引用:
原帖由 wjhere 于 2009-3-21 13:16 发表'测试文档为朱自清先生的《春》
'根据sylun兄提供KSPPtoASCII3修改
'sylun兄已经发给我最快的测试文档仅3秒多。(因电脑而异)没有得到授权,不宜公开代码
'得到了论坛朋友们的帮助。这个是原来的改进版,改用倒序 ...
Sub hz2py()
'无选定区域则对全文档的汉字添加拼音
Application.ScreenUpdating = False
On Error Resume Next
Dim oRange As Range, i As Range, info() As String, n As Long
Dim j As Range, c As Long, TF As Boolean
Dim st As Single
st = Timer
Set oRange = VBA.IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
With oRange
For Each i In .Words
If TF = True Then
ActiveDocument.Undo
i.Start = i.Start + 1
TF = False
End If
If i.Text Like "[一-龥]*" Then
ReDim Preserve info(n)
info(n) = HzToPy(i.Text) & "|" & i.Start & "|" & i.End
n = n + 1
ElseIf Len(i.Text) > 1 And i.Characters.Last Like "[”》]" Then
i.Characters.Last.Delete
TF = True
For Each j In i.Words
If j.Text Like "[一-龥]*" Then
ReDim Preserve info(n)
info(n) = HzToPy(j.Text) & "|" & j.Start & "|" & j.End
n = n + 1
End If
Next
ElseIf IsDate(i.Text) Then
For Each j In i.Characters
If j.Text Like "[一-龥]" Then
ReDim Preserve info(n)
info(n) = HzToPy(j.Text) & "|" & j.Start & "|" & j.End
n = n + 1
End If
Next
End If
Next
End With
For c = UBound(info) To 0 Step -1
ActiveDocument.Range(Split(info(c), "|")(1), Split(info(c), "|")(2)).PhoneticGuide Text:=Split(info(c), "|")(0), FontSize:=10, Raise:=13
If c = 0 Then Exit For
Next
If TF = True Then ActiveDocument.Undo
Debug.Print Timer - st
MsgBox Timer - st
Application.ScreenUpdating = True
End Sub
[ 本帖最后由 sylun 于 2009-3-21 21:00 编辑 ]
作者: sylun 发布时间: 2009-03-21

作者: coby001 发布时间: 2009-03-21
作者: wjhere 发布时间: 2009-03-21
作者: lcmphy 发布时间: 2009-06-07
作者: yanjie 发布时间: 2009-07-24
作者: pangz 发布时间: 2009-11-06
作者: akcbs 发布时间: 2010-04-26
作者: wbz2cdma 发布时间: 2010-05-07

作者: paulqi 发布时间: 2010-08-26
作者: zhenghua9256 发布时间: 2010-08-27
作者: excel5.0 发布时间: 2011-01-05
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28