【简 介】如果我们需要把Word文档中的全角数字全部改为半角数字,手头又没有合适的转换软件,应该怎么做呢? 
如果我们需要把Word文档中的全角数字全部改为半角数字,手头又没有合适的转换软件,应该怎么做呢?
打开Word的“查与替换”,怎么也不到全角改半角的有关设置。执行十次替换虽然可以达到目的,可以步骤也过于繁琐。思前想后,决定用“宏”这个杀手锏。
1.执行“工具”→“宏”→“录制新宏...”,在“宏名”框中输入“数字全角转半角”,确定,将出现一个“录制宏”工具栏,关闭它。
2.执行“工具”→“宏”→“宏...”,选择“数字全角转半角”,单击“编辑”按钮,打开vba编辑窗口,录入代码结果如下:
Sub 数字全角转半角()
'使用前需先选中要替换的区域
Dim qjsz, bjsz As String, i As Integer '定义qjsz(全角数字)、bjsz(半角数字)为字符串型,i为整数型
qjsz = "0123456789"
bjsz = "0123456789"
For i = 1 To 10 '循环10次
With Selection.Find
.Text = Mid(qjsz, i, 1) 'mid函数:返回文本字符串中从指定位置开始的特定数目的字符,每次取一个数字
.Replacement.Text = Mid(bjsz, i, 1) '将用于替换的相应位置的半角数字
.Format = False '保留替换前的字符格式
.Execute Replace:=wdReplaceAll '用半角数字替换全角数字
End With
Next i
End Sub
3.关闭vba编辑窗口,选中要改变数字格式的文字区域,执行“工具”→“宏”→“宏...”→“数字全角转半角”。一切OK!
原创]中英标点转换
说明:由于输入法切换等问题,中英文的标点符号在WORD中的替换,一直没有很好地由代码进行.这次应网友要求,作了一个比较详尽的替换方案.
1.        中文标点符号范围(非全部):
"。", ",", ";", ":", "?", "!", "……", "—", "~", "〔", "〕", "《", "》", "‘", "’", "“", "”"共17个
2.        英文标点符号范围(非全部),对应中文标点符号:
".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">", "'", "'", """", """"
最后四个相当于',',",".
3.        在本方案中,制作了三个宏过程:
a)        ReplaceEnglishInterpunctionInChinese:适用于中英互译文章中的中文段落中的英文标点符号自动全部转化为中文标点符号(是不是有些绕口),您可能需要注意的是,中文段落的起始字符必须为中文汉字,不得为空格等,以示区分.简体中文的ASCII码在-2050到-20319之间,这是代码运行的判断依据.
b)        ReplaceInStoryChinese:适用于全文档的英文标点符号替换为中文标点符号.即不再区分英文段落还是中文段落.
c)        ReplaceChineseI
nterpunctionInEnglish:全中文标点符号替换为英文标点符号.
4.        注意事项:
如果对于中大型文字文档(如中英互译书籍),第一个代码运行时,需要较高电脑配置和一定运行时间.实点显示:电脑配置:P4,1.8G,256M,WINDOWS2000,OFFICE XP ,处理文档大小:465K,总页数43页.运行时间8分钟.您可能需要关闭所有与程序运行无关的窗口.
相关链接 lhome/viewthread.php?tid=98760
如果您还有一些特殊要求,我也可以另行设计代码(主体结构相似,或许您可以直接修改代码)
以下代码供参考:
'* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-5-20 5:30:30
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------
Sub ReplaceEnglishInterpunctionInChinese()
'中英互译文档中将中文段落中的英文标点符号替换为中文标点符号
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 13    '进行14次循环
Set MyRange = i.Range    '定义一个RANGE对象
With MyRange.Find    '查
.ClearFormatting    '清除查格式
'查相应的英文标点,替换为对应的中文标点
.
Execute findtext:=EnglishInterpunction(N), replacewith:=ChineseInterpunction(N), Replace:=wdReplaceAll
End With
字符串数组怎么转成byteNext
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
'----------------------
Sub ReplaceInStoryChinese()
'全中文段落英文标点符号替换为中文标点符号
Dim i As Paragraph, ChineseInterpunction() As Variant, EnglishInterpunction() As Variant
Dim N As Byte
'定义一个中文标点的数组对象
ChineseInterpunction = Array("。", ",", ";", ":", "?", "!", "……", "—", "~", "〔", "〕", "《", "》", "‘", "’", "“", "”")
'定义一个英文标点的数组对象
EnglishInterpunction = Array(".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">", "'", "'", """", """")
On Error Resume Next
Application.ScreenUpdating = False    '关闭屏幕更新
With ThisDocument.Content.Find
For N = 0 To 13    '进行14次循环
.ClearFormatting    '清除查格式
'查相应的英文标点,替换为对应的中文标点
.
Execute findtext:=EnglishInterpunction(N), replacewith:=ChineseInterpunction(N), Replace:=wdReplaceAll
Next
End With
Selection.HomeKey wdStory    '移到文档首
With Selection.Find
.ClearFormatting    '清除查格式
.Text = """"    '查"
'如果查成功并且在中文段落中,分别将其替换为“/”
While .Execute: Selection.Text = "“"
.Execute: Selection.Text = "”"
Wend
End With
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting    '清除查格式
.Text = "'"    '查'
'如果查成功并且在中文段落中,分别将其替换为‘/’
While .Execute: Selection.Text = "‘"
.Execute: Selection.Text = "’"
Wend
End With
'恢复屏幕更新
Application.ScreenUpdating = True
End Sub
'----------------------
Sub ReplaceChineseInterpunctionInEnglish()
'全中文标点符号替换为英文标点符号
Dim ChineseInterpunction() As Variant, EnglishInterpunction() As Variant, N As Byte
'定义一个中文标点的数组对象
ChineseInterpunction = Array("。", ",", ";", ":", "?", "!", "……", "—", "~", "〔", "〕", "《", "》", "‘", "’", "“", "”")
'定义一个英文标点的数组对象
EnglishInterpunction = Array(".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">", "'", "'", """", """")
On Error Resume Next
MsgBox UBound(EnglishInterpunction)
Application.ScreenUpdating = False    '关闭屏幕更新
With ThisDocument.Content.Find
For
N = 0 To 16    '进行14次循环
.ClearFormatting    '清除查格式
'查相应的英文标点,替换为对应的中文标点
.Execute findtext:=ChineseInterpunction(N), replacewith:=EnglishInterpunction(N), Replace:=wdReplaceAll
Next
End With
'恢复屏幕更新
Application.ScreenUpdating = True
End Sub
'----------------------
word 排版技巧 之 批量标点转换: WORD中批量转换中英文标点符号knowledge 2008-07-24 16:07:46 阅读1146 评论0 字号:大中 小 这个东西了两次了,还是保持下来好! 先记下一个东西:全角情况下,WORD中的空格为方格形状;半角情况下,空格为小黑
     WORD中批量转换中英文标点符号knowledge 2008-07-24 16:07:46 阅读1146 评论0 字号:大中小
  这个东西了两次了,还是保持下来好!
  先记下一个东西:全角情况下,WORD中的空格为方格形状;半角情况下,空格为小黑点形状。
  打开word,"工具"→"宏"→"Visual Basic 编辑器",在主窗体复制以下内容后粘贴,保存.关闭编辑器,在"工具"→"宏"→"宏"里调用.
  Sub ToggleInterpunction()'中英文标点互换
  Dim ChineseInterpunction() As Variant, EnglishInterpunction() As Variant
  Dim myArray1() As Variant, myArray2() As Variant, strFind As String, strRep As String
  Dim msgResult As VbMsgBoxResult, N As Byte
  '定义一个中文标点的数组对象
  ChineseInterpunction = Array("、","。", ",", ";", ":", "?", "!", "……", "—", "~", "(", ")", "《", "》")
  '定义一个英文标点的数组对象
  EnglishInterpunction = Array(",",".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">")
  '提示用户交互的MSGBOX对话框
  msgResult = MsgBox("您想中英标点互换吗?按Y将中文标点转为英文标点,按N将英文标点转为中文标点!", vbYesNoCancel)
  Select Case msgResult
  Case vbCancel
  Exit Sub '如果用户选择了取消按钮,则退出程序运行
  Case vbYes '如果用户选择了YES,则将中文标点转换为英文标点
  myArray1 = ChineseInterpunction
  myArray2 = EnglishInterpunction
  strFind = "“(*)”"
  strRep = """\1"""
  Case vbNo '如果用户选择了NO,则将英文标点转换为中文标点
  myArray1 = EnglishInterpunction
  myArray2 = ChineseInterpunction
  strFind = """(*)"""
  strRep = "“\1”"
  End Select
  Application.ScreenUpdating = False '关闭屏幕更新
  For N = 0 To UBound(ChineseInterpunction) '从数组的下标到上标间作一个循环
  With ActiveDocument.Content.Find
  .ClearFormatting '不限定查格式
  .MatchWildcards = False '不使用通配符
  '查相应的英文标点,替换为对
应的中文标点
  .Execute findtext:=myArray1(N), replacewith:=myArray2(N), Replace:=wdReplaceAll
  End With
  Next
  With ActiveDocument.Content.Find
  .ClearFormatting '不限定查格式
  .MatchWildcards = True '使用通配符
  .Execute findtext:=strFind, replacewith:=strRep, Replace:=wdReplaceAll
  End With
  Application.ScreenUpdating = True '恢复屏幕更新

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。