word中利用宏替换标点标点全角与半角
2024-09-30 06:43:10
Alt+F11,然后插入-模块:
复制下面代码到编辑窗口:
Sub 半角标点符号转换为全角标点符号()
'中英互译文档中将中文段落中的英文标点符号替换为中文标点符号
Dim i As Paragraph, ChineseInterpunction() As Variant, EnglishInterpunction() As Variant
Dim MyRange As Range, N As Byte
'定义一个中文标点的数组对象
ChineseInterpunction = Array("。", ",", ";", ":", "?", "!", "……", "—", "~", "〔", "〕", "《", "》", "‘", "’", "“", "”")
'定义一个英文标点的数组对象
EnglishInterpunction = Array(".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">", "'", "'", """", """")
On Error Resume Next
Application.ScreenUpdating = False '关闭屏幕更新
For Each i In ThisDocument.Paragraphs '遍历文档每个段落
If Asc(i.Range) < Then '如果段落首个字符为汉字(汉字字符的ASC<0)
'定义一个RANGE对象
For N = To '进行14次循环
Set MyRange = i.Range '定义一个RANGE对象
With MyRange.Find '查找
.ClearFormatting '清除查找格式
'查找相应的英文标点,替换为对应的中文标点
.Execute findtext:=EnglishInterpunction(N), replacewith:=ChineseInterpunction(N), Replace:=wdReplaceAll
End With
Next
End If
Next
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting '清除查找格式
.Text = """" '查找"
'如果查找成功并且在中文段落中,分别将其替换为“/”
While .Execute
If Asc(Selection.Paragraphs().Range) < Then Selection.Text = "“"
If .Execute And Asc(Selection.Paragraphs().Range) < Then Selection.Text = "”"
Wend
End With
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting '清除查找格式
.Text = "'" '查找'
While .Execute
'如果查找成功并且在中文段落中,分别将其替换为‘/’
If Asc(Selection.Paragraphs().Range) < Then Selection.Text = "‘"
If .Execute And Asc(Selection.Paragraphs().Range) < Then Selection.Text = "’"
Wend
End With
'恢复屏幕更新
Application.ScreenUpdating = True
End Sub
然后Alt+F8,选择刚刚添加的宏,并运行:
---分割线---
还有另一段代码,简洁一些:
Sub 全角转换为半角()
'使用前需先选中要替换的区域
Dim fullshape, halfshape As String, i As Integer '定义fullshape(全角)、halfshape(半角)为字符串型,i为整数型
fullshape = ",。?“”‘’!:;"
halfshape = ",.?""''!:;"
For i = To '循环10次
With Selection.Find
.Text = Mid(fullshape, i, ) 'mid函数:返回文本字符串中从指定位置开始的特定数目的字符,每次取一个标点符号
.Replacement.Text = Mid(halfshape, i, ) '将用于替换的相应位置的半角标点符号
.Format = False '保留替换前的字符格式
.Execute Replace:=wdReplaceAll '用半角标点替换全角标点
End With
Next i
End Sub
最新文章
- Webpack教程
- php 微信公众平台上传多媒体接口 41005错误
- javascript设计模式-享元模式
- POJ1037A decorative fence(好dp)
- innodb_support_xa
- Sicily 1936. Knight Moves
- 在VirtualBox虚拟机上采集Fedora15系统
- js理解
- Hibernate之AbstractEntityPersister
- CCNA网络工程师学习进程(7)路由器的路由配置
- Java GC - 垃圾回收机制
- HTA基础
- 安装inotify-tools监控工具
- 原生JS实现轮播效果
- Born Slippy (超大背包问题 + 树形DP)
- 在学习DRF之前
- 开源的挖矿软件,sha256
- Oracle11g 表空间使用监控
- NC入门笔记
- Mac OS 10.12 - 解决“bad interpreter: No such file or directory”问题!
热门文章
- PAT (Advanced Level) 1076. Forwards on Weibo (30)
- HDU 5521 Meeting
- 使用Visual Studio 2008创建你的第一个Windows Mobile程序介绍
- CodeForces 610D Vika and Segments
- hearbeart
- jQuery方式事件冒泡的2个方法
- 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- 使用msm文件创建msi
- (简单) POJ 1797 Heavy Transportation,Dijkstra。
- 关于div宽度和高度的100%设定