Word半角标点转全角标点的VBA

打开Word文档。

按Alt+F11打开VBA编辑器。

点击插入→模块。

右侧空白处输入以下代码:

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) < 0 Then '如果段落首个字符为汉字(汉字字符的ASC<0)
'定义一个RANGE对象
For N = 0 To 14 '进行15次循环
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(1).Range) < 0 Then Selection.Text = "“"
If .Execute And Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "”"
Wend
End With
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting '清除查找格式
.Text = "'" '查找'
While .Execute
'如果查找成功并且在中文段落中,分别将其替换为‘/’
If Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "‘"
If .Execute And Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "’"
Wend
End With
'恢复屏幕更新
Application.ScreenUpdating = True
End Sub

点右上角叉号关闭VBA编辑器。

回Word文档,按Alt+F8。

左侧选择“半角标点符号转换为全角标点符号”的宏,点右侧运行。

会把英文标点的数组对象中的英文标点转化为中文标点的数组对象中的标点。

对应的标点可以自行编辑,但如果需要增加减少的话,需要修改For循环语句中的循环次数,和定义的标点数量一致即可。

原代码参考这里